sql server - SQL Case Statement (Understanding) -


i ran across case statement in database. i'm trying figure out does. case statement trying forecast sum of toys week. below case statement can break down , explain me.

case      when row_number() over(order datetbl.[date]) <> count(*) over()      null      else isnull(case          when case              when row_number() over(order datetbl.[date]) = count(*) over()              (sum([toy])/(datepart(dw,getdate())-1))*7          end = 0          sum(toy) + 1          else case              when row_number() over(order datetbl.[date]) = count(*) over()              (sum([toy])/(datepart(dw,getdate())-1))*7          end      end,0)  end [toyforcast] 

thanks

i've added comments query try break down bit bit.

case      when row_number() over(order datetbl.[date]) <> count(*) over() --this checks if on last row of dataset, if not => put null in column     null      else isnull(case          when case              when row_number() over(order datetbl.[date]) = count(*) over() --on last row             (sum([toy])/(datepart(dw,getdate())-1))*7 --find average of toys far week extrapolated out end of week (presumably figure out how many toys used(?) end of week         end = 0 --if there going 0 toys used end of week show sum(refire) + 1, whatever signifies         sum(refire) + 1          else case              when row_number() over(order datetbl.[date]) = count(*) over() --again on last row             (sum([toy])/(datepart(dw,getdate())-1))*7 --show extrapolated value using average used per day far         end      end,0) --if after of ended null value extrapolated value, replace null 0 end [toyforcast] 

basically tries run calculation on last row of dataset assume report. tries figure out toys value end of week calculating average toys per day week , multiplying 7 (to week end projection). if projection results in 0, put sum of refire + 1 projection (no clue mean). if ended null because have no data, put in 0 instead.

hopefully helps , brain didn't miss something.


Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -