--下面是测试
declare @表A table(number int,categoryid int)
insert into @表A
select 20,1
union all select 30,1
union all select 50,2
union all select 60,1
union all select 80,2
--查询
select number from(
select * from @表A
union all
select number=sum(number),categoryid
from @表A
group by categoryid
) a order by categoryid,number
/*--测试结果
number
-----------
20
30
60
110
50
80
130
SELECT CASE WHEN (GROUPING(categoryid) = 1) THEN 'ALL'
ELSE ISNULL(categoryid, 'UNKNOWN')
END AS categoryid,
SUM(number) AS number
FROM 表A
GROUP BY categoryid WITH ROLLUP