我在表中有一些记录,我希望显示该表中的所有记录,之后我想显示相应列的总和那么这个问题的sql查询是什么? Example 请在附件中找到输入和预期输出。
由于
答案 0 :(得分:0)
使用ROLLUP:
select coalesce(category, 'Total') as Category,
sum(a) as A,
. . .
from table
group by rollup(category);
答案 1 :(得分:0)
测试:http://rextester.com/HUE40725
这可以帮到你:
SELECT Category = CASE WHEN GROUPING(Category) = 0 THEN Category ELSE 'Total' END,
A = SUM(A),
B=SUM(B),
C = SUM(C),
D=SUM(D),
Total = SUM(Total)
FROM dbo.NameTable
GROUP BY Category WITH ROLLUP
HAVING GROUPING(Category) = 0
OR GROUPING(Category) = 1
;