首页 > 代码库 > sqlservere小计合计总计

sqlservere小计合计总计

SELECT CASE WHEN GROUPING(F1) = 1 THEN ‘总计‘
WHEN GROUPING(F1) = 0 AND GROUPING(F2) = 1 THEN F1+‘合计‘
WHEN GROUPING(F1) = 0 AND GROUPING(F2) = 0 AND GROUPING(F3) = 1 THEN F1+‘的‘+F2+‘小计‘
ELSE F1
END AS F1 ,
CASE WHEN GROUPING(F3) = 1 THEN ‘‘ ELSE F2
END AS F2 ,
ISNULL(F3, ‘‘) AS F3 ,
SUM(F4) AS F4,
SUM(F5) AS F5
FROM TB
GROUP BY F1,F2,F3 WITH ROLLUP;

sqlservere小计合计总计