t1:
dt decimal(8,0),
amt decimal(15,2),
amt1 decimal(15,2)
原数据:
200601 1 0
200602 2 0
200603 3 0
200604 4 0
200605 5 0
200606 6 0
200607 7 0
200608 8 0
200609 9 0
200610 10 0
200611 11 0
200612 12 0
200701 2 0
200702 5 0
希望结果:
200601 1 1
200602 2 1
200603 3 1
200604 4 1
200605 5 1
200606 6 1
200607 7 1
200608 8 1
200609 9 1
200610 10 1
200611 11 1
200612 12 1
200701 2 2
200702 5 3
一月份AMT放入AMT1,其他是后月AMT减上月AMT放入AMT1
dt decimal(8,0),
amt decimal(15,2),
amt1 decimal(15,2)
原数据:
200601 1 0
200602 2 0
200603 3 0
200604 4 0
200605 5 0
200606 6 0
200607 7 0
200608 8 0
200609 9 0
200610 10 0
200611 11 0
200612 12 0
200701 2 0
200702 5 0
希望结果:
200601 1 1
200602 2 1
200603 3 1
200604 4 1
200605 5 1
200606 6 1
200607 7 1
200608 8 1
200609 9 1
200610 10 1
200611 11 1
200612 12 1
200701 2 2
200702 5 3
一月份AMT放入AMT1,其他是后月AMT减上月AMT放入AMT1
insert into @t select 200601, 1,0
insert into @t select 200602, 2,0
insert into @t select 200603, 3,0
insert into @t select 200604, 4,0
insert into @t select 200605, 5,0
insert into @t select 200606, 6,0
insert into @t select 200607, 7,0
insert into @t select 200608, 8,0
insert into @t select 200609, 9,0
insert into @t select 200610,10,0
insert into @t select 200611,11,0
insert into @t select 200612,12,0
insert into @t select 200701, 2,0
insert into @t select 200702, 5,0select
dt,amt,amt-isnull((select amt from @t where dt=t.dt-1),0) as amt1
from
@t t
insert into @t select 200601, 1,0
insert into @t select 200602, 2,0
insert into @t select 200603, 3,0
insert into @t select 200604, 4,0
insert into @t select 200605, 5,0
insert into @t select 200606, 6,0
insert into @t select 200607, 7,0
insert into @t select 200608, 8,0
insert into @t select 200609, 9,0
insert into @t select 200610,10,0
insert into @t select 200611,11,0
insert into @t select 200612,12,0
insert into @t select 200701, 2,0
insert into @t select 200702, 5,0select
dt,amt,amt-isnull((select amt from @t where dt=t.dt-1),0) as amt1
from
@t t/*
dt amt amt1
----------- ----------- -----------
200601 1 1
200602 2 1
200603 3 1
200604 4 1
200605 5 1
200606 6 1
200607 7 1
200608 8 1
200609 9 1
200610 10 1
200611 11 1
200612 12 1
200701 2 2
200702 5 3
*/update t
set
amt1=amt-isnull((select amt from @t where dt=t.dt-1),0)
from
@t tselect * from @t/*
dt amt amt1
----------- ----------- -----------
200601 1 1
200602 2 1
200603 3 1
200604 4 1
200605 5 1
200606 6 1
200607 7 1
200608 8 1
200609 9 1
200610 10 1
200611 11 1
200612 12 1
200701 2 2
200702 5 3
*/