select * INTO #temp from ( select 1 as id,'1/1/2015' as actualdate, 1111 as B1trade,2222 b2trade, 23.56 as Net,Null as Age UNION select 2 as id,'1/2/2015' as actualdate, 1111 as B1trade,2222 b2trade,23.56 as Net, Null as Age UNION select 3 as id,'1/3/2015' as actualdate, 1111 as B1trade,2222 b2trade,0 as Net, Null as Age UNION select 4 as id,'1/4/2015' as actualdate, 1111 as B1trade,2222 b2trade,23.56 as Net, Null as Age UNION select 5 as id,'1/5/2015' as actualdate, 1111 as B1trade,2222 b2trade,23.56 as Net, Null as Age )a select * from #temp
Age is the new column added to the exisiting table, need to calculate the age based on NET value
My age calculation should start from 1/1/2015, there is a data int he table prior to 2015 as well but we dont need the age of that.
My requirement is whenever there
is anet value >1 or <0 then i should give the age as 1 and if the net is still not zero for the next day, the age should increase by 1 until i see the zero net. For the next NET value it should start with 1 again
so
ID AGE
1 1
2 2
3 0
4 1
5 2
unique key is actualdate + B1trade+b2trade
I am using a cursor to calculate , but i am not getting how assign the value for first record
Please help me