SQL Server: value divided by rolling sum of last four values? -
how can value divided last 4 values in sql server 2014?
example. how can valuedivbylastfourvalues field value field?
month|value| valuedivbylastfourvalues | --------------------------------------- 1 | 1 | 1 | 2 | 1 | 1/2 | 3 | 2 | 2/4 | 4 | 2 | 2/6 | 5 | 1 | 1/6 | 6 | 7 | 7/12 | 7 | 4 | 4/14 | bad trial
select customer ,month ,net_value_m / sum(net_value_m) 'net_value_m_prop_by_year' data group left(month,4) ,customer ,month ,net_value_m order net_value_m_prop_by_year
if understand correctly:
select customer, month, (net_value_m / sum(net_value_m) on (order month rows between 4 preceding , current row) ) net_value_m_prop_by_year data; this uses window function sum values in current row , preceding 4 rows.
Comments
Post a Comment