use test1
go
declare @a table (数量 int,种类 nvarchar(100))
insert into @a(数量,种类)
select 1,'手机' union all
select 2,'电脑' union all
select 3,'手机' union all
select 4,'电脑' union all
select 5,'电脑'
select 种类,数量 from @a
select
DENSE_RANK() over (order by A.种类) as New_ID,
A.种类,
(select SUM(数量) from @a where 种类=A.种类) as 总数量
from (
select 种类, COUNT(种类) as 种类行数 from @a group by 种类) as A
初始:
种类 数量
手机 1
电脑 2
手机 3
电脑 4
电脑 5
结果:
New_ID 种类 总数量
1 电脑 11
2 手机 4