sql - Insert new column that adds a series from another column -
i have table dates , follower growth each date , want make new column depicts total followers gained.
how can insert new column adds 'new followers' column in summation series?
date | new followers ----------------------- 1/1/15 | 1 1/2/15 | 3 1/3/15 | 5 1/4/15 | 4 1/5/15 | 3
new table like
date | new followers | total followers gained ------------------------------------------------ 1/1/15 | 1 | 1 1/2/15 | 3 | 4 1/3/15 | 5 | 9 1/4/15 | 4 | 13 1/5/15 | 3 | 16
this sql accomplish want select. if necessary persist value in table, use sql in trigger. since calculate amount on fly, i'm not sure why persist it.
declare @temptable table (thedt datetime, newfollowers int) insert @temptable select '1/1/15',1 union select '1/2/15',3 union select '1/3/15',5 union select '1/4/15',4 union select '1/5/15',3 select t1.thedt, t1.newfollowers + sum(case when t2.thedt not null t2.newfollowers else 0 end) "total followers" @temptable t1 left outer join @temptable t2 on t1.thedt > t2.thedt group t1.thedt, t1.newfollowers order t1.thedt
Comments
Post a Comment