50分求 sql
表T1 数据
userid type num
1000 1 2
1000 1 3
1000 2 1
1000 2 1
1000 2 1
1001 1 1
1001 1 1
怎么合并成
userid type num
1000 1 5
1000 2 3
1001 1 2
------解决方案-------------------- select userid,type, sum(num) num
into #T
from
(
select 1000 userid, 1 type,2 num union all
select 1000, 1,3 union all
select 1000, 2,1 union all
select 1000, 2,1 union all
select 1000, 2,1 union all
select 1001, 1,1 union all
select 1001,1,1 ) A group by userid ,type order by userid
select * from #T
更新不行,应该写入另一个表。之后把表T1表相应的记录删除。
不过怎么会有T1表这样的数据,设计表有问题吧?
------解决方案--------------------select userid,type,sum(num) as num
into #
from T1 group by userid,type
truncate table T1
insert T1 select * from #
drop table #