22,209
社区成员
发帖
与我相关
我的任务
分享
结果是:
01 11
02 5
还是
01 18
02 4
?
自己把结果贴清楚
select [name],sum(beishu) from tb where [name]<>1 group by [name]
declare @a table ([name] varchar(2),beishu int)
insert into @a
select '01', 2 union all
select '01', 9 union all
select '02', 4 union all
select '02', 1
select a.[name],sum(counts*beishu) as zongji
from
(select [name],count(name) as counts
from @a
group by [name])a
inner join @a b on a.[name] = b.[name]
group by a.[name]
/*结果
name zongji
---- -----------
01 22
02 10
*/