sql - How to update based on previous record -
i have have data below:
coursekey userkey userspk1 msgmain date cummcount recent count 32332 33 33 2016-08-22 2 0 32332 33 33 2016-08-24 6 0
i wanting say:
update table set recent count = (cummcount latest date - cummcount previous date);
in case 4.
i confused on how capture previous row using date in case
use lag
:
with cte ( select [msgmain date], cummcount - lag(cummcount, 1, 0) on (order [msgmain date]) newcount ff ) update ff set [recent count] = cte.newcount ff inner join cte on ff.[msgmain date] = cte.[msgmain date]
Comments
Post a Comment