你可以添加 WITH ROLLUP 你的条款 GROUP BY 它会在输出的末尾添加一个聚合行,即
WITH ROLLUP
GROUP BY
SELECT SUM(col1), SUM(col2)... FROM table GROUP BY class WITH ROLLUP
您尚未提供要检查的样本数据,但可以使用一种方法 CASE WHEN 在 GROUP BY 。
CASE WHEN
以下 UNION
UNION
SELECT Sum(col1) FROM tablename WHERE id <> 1 UNION SELECT Sum(col1) FROM tablename WHERE id = 1 GROUP BY class
可以写成如下使用 CASE
CASE
SELECT Sum(col1) FROM tablename GROUP BY CASE WHEN id = 1 THEN 0 ELSE 1 END