34,587
社区成员
发帖
与我相关
我的任务
分享
declare @tb_1 table
(
type int,
num int
)
insert into @tb_1
select 1,10
union
select 2,15
union
select 3,25
union
select 4,30
declare @tb_2 table
(
ID int ,
f0010 int,
f0020 int,
f0030 int
)
insert into @tb_2 (ID)
select 1
union
select 2
union
select 3
union
select 4
union
select 5
union
select 6
union
select 7
select * from @tb_2
select case when type=1 then 1
when type in(2,3) then 2
when type=4 then 3 end as type,sum(num) as sum_num
from @tb_1
group by case when type=1 then 1
when type in(2,3) then 2
when type=4 then 3 end
update a set a.f0010=case when b.type=1 then b.sum_num end,
a.f0020=case when b.type=2 then b.sum_num end,
a.f0030=case when b.type=3 then b.sum_num end
from @tb_2 as a,(select case when type=1 then 1
when type in(2,3) then 2
when type=4 then 3 end as type,sum(num) as sum_num
from @tb_1
group by case when type=1 then 1
when type in(2,3) then 2
when type=4 then 3 end
) as b
update @tb_2 set f0010=a.num,f0020=b.num,f0030=c.num
from (select num from @tb_1 where type=1)a,
(select num from @tb_1 where type=2)b,
(select num from @tb_1 where type=3)c
declare @tb_1 table
(
type int,
num int
)
insert into @tb_1
select 1,10
union
select 2,15
union
select 3,25
declare @tb_2 table
(
ID int ,
f0010 int,
f0020 int,
f0030 int
)
insert into @tb_2 (ID)
select 1
union
select 2
union
select 3
union
select 4
union
select 5
union
select 6
union
select 7
update @tb_2 set
f0010=(select num from @tb_1 where type=1),
f0020=(select num from @tb_1 where type=2),
f0030=(select num from @tb_1 where type=3)
select * from @tb_2
update @tb_2 set
f0010=(select num from @tb_1 where type=1),
f0020=(select num from @tb_1 where type=2),
f0030=(select num from @tb_1 where type=3)