22,297
社区成员
发帖
与我相关
我的任务
分享
--测试数据
if object_id('tb') is not null
drop table tb
create table tb(id char(10),yc dec(18,2),ym dec(18,2),type_begin dec(18,2),type_end dec(18,2),month int,code int)
insert into tb
select '101',100.00,120.00,150.00,null,1 ,1 union all
select '101',130.00 ,30.00 ,null,null,2 ,1 union all
select '101',110.00 ,20.00 ,null,null,3 ,1 union all
select '101',170.00 ,70.00 ,null,null,4 ,1 union all
select '101',150.00 ,40.00 ,null,null,5 ,1 union all
select '101',120.00 ,80.00 ,null,null,6 ,1 union all
select '501',1 ,2,5,null,1,1 union all
select '501',2,3 ,null,null,2 ,1 union all
select '501',5,8 ,null,null,3 ,1 union all
select '501',10,12 ,null,null,4 ,1 union all
select '501',32,13 ,null,null,5 ,1 union all
select '501',22,3 ,null,null,6 ,1 union all
select '20201',5 ,7,12,null,1 ,9999 union all
select '20201',13,12,null,null,2,9999 union all
select '20201',23,18,null,null,3,9999 union all
select '20201',15,18,null,null,4,9999 union all
select '20201',10,19,null,null,5,9999 union all
select '20201',16,17,null,null,6,9999 union all
select '20201',16,17,12,null,1,1100 union all
select '20201',13,12,null,null,2,1100 union all
select '20201',23,18,null,null,3,1100 union all
select '20201',15,18,null,null,4,1100 union all
select '20201',10,19,null,null,5,1100 union all
select '20201',16,17,null,null,6,1100
declare @i int
set @i = 2
update tb set type_end = case when substring(id,1,1) in ('1','5') then type_begin +YC - YM
else type_begin +YM-YC end
while @i <= 12
begin
update tb set type_begin = case month when 1 then type_begin
else (select type_end from tb where id = t.id and month = @i - 1 and code = t.code) end
from tb t
where month = @i
update tb set type_end = case when substring(id,1,1) in ('1','5') then type_begin +YC - YM
else type_begin +YM-YC end
set @i = @i + 1
end
select * from tb
drop table tb
--结果
--------------------------
101 100.00 120.00 150.00 130.00 1 1
101 130.00 30.00 130.00 230.00 2 1
101 110.00 20.00 230.00 320.00 3 1
101 170.00 70.00 320.00 420.00 4 1
101 150.00 40.00 420.00 530.00 5 1
101 120.00 80.00 530.00 570.00 6 1
501 1.00 2.00 5.00 4.00 1 1
501 2.00 3.00 4.00 3.00 2 1
501 5.00 8.00 3.00 .00 3 1
501 10.00 12.00 .00 -2.00 4 1
501 32.00 13.00 -2.00 17.00 5 1
501 22.00 3.00 17.00 36.00 6 1
20201 5.00 7.00 12.00 14.00 1 9999
20201 13.00 12.00 14.00 13.00 2 9999
20201 23.00 18.00 13.00 8.00 3 9999
20201 15.00 18.00 8.00 11.00 4 9999
20201 10.00 19.00 11.00 20.00 5 9999
20201 16.00 17.00 20.00 21.00 6 9999
20201 16.00 17.00 12.00 13.00 1 1100
20201 13.00 12.00 13.00 12.00 2 1100
20201 23.00 18.00 12.00 7.00 3 1100
20201 15.00 18.00 7.00 10.00 4 1100
20201 10.00 19.00 10.00 19.00 5 1100
20201 16.00 17.00 19.00 20.00 6 1100
--测试数据
if object_id('tb') is not null
drop table tb
create table tb(id char(10),yc dec(18,2),ym dec(18,2),type_begin dec(18,2),type_end dec(18,2),month int,code int)
insert into tb
select '101',100.00,120.00,150.00,null,1 ,1 union all
select '101',130.00 ,30.00 ,null,null,2 ,1 union all
select '101',110.00 ,20.00 ,null,null,3 ,1 union all
select '101',170.00 ,70.00 ,null,null,4 ,1 union all
select '101',150.00 ,40.00 ,null,null,5 ,1 union all
select '101',120.00 ,80.00 ,null,null,6 ,1 union all
select '501',1 ,2,5,null,1,1 union all
select '501',2,3 ,null,null,2 ,1 union all
select '501',5,8 ,null,null,3 ,1 union all
select '501',10,12 ,null,null,4 ,1 union all
select '501',32,13 ,null,null,5 ,1 union all
select '501',22,3 ,null,null,6 ,1 union all
select '20201',5 ,7,12,null,1 ,9999 union all
select '20201',13,12,null,null,2,1100 union all
select '20201',23,18,null,null,3,1100 union all
select '20201',15,18,null,null,4,1100 union all
select '20201',10,19,null,null,5,1100 union all
select '20201',16,17,null,null,6,1100
--过程
declare @i int
set @i = 2
update tb set type_end = case when substring(id,1,1) in ('1','5') then type_begin +YC - YM
else type_begin +YM-YC end
while @i <= 12
begin
update tb set type_begin = case month when 1 then type_begin
else (select type_end from tb where id = t.id and month = @i - 1) end
from tb t
where month = @i
update tb set type_end = case when substring(id,1,1) in ('1','5') then type_begin +YC - YM
else type_begin +YM-YC end
set @i = @i + 1
end
select * from tb
drop table tb
--结果
--------------------------
101 100.00 120.00 150.00 130.00 1 1
101 130.00 30.00 130.00 230.00 2 1
101 110.00 20.00 230.00 320.00 3 1
101 170.00 70.00 320.00 420.00 4 1
101 150.00 40.00 420.00 530.00 5 1
101 120.00 80.00 530.00 570.00 6 1
501 1.00 2.00 5.00 4.00 1 1
501 2.00 3.00 4.00 3.00 2 1
501 5.00 8.00 3.00 .00 3 1
501 10.00 12.00 .00 -2.00 4 1
501 32.00 13.00 -2.00 17.00 5 1
501 22.00 3.00 17.00 36.00 6 1
20201 5.00 7.00 12.00 14.00 1 9999
20201 13.00 12.00 14.00 13.00 2 1100
20201 23.00 18.00 13.00 8.00 3 1100
20201 15.00 18.00 8.00 11.00 4 1100
20201 10.00 19.00 11.00 20.00 5 1100
20201 16.00 17.00 20.00 21.00 6 1100