[英]How to add 'total' row at bottom of table in SQL?
I have a pivot table with a totals column but am having trouble making a totals row at the bottom of the table using SQL. This is the code I currently have:我有一个带有总计列的 pivot 表,但我无法使用 SQL 在表底部制作总计行。这是我目前拥有的代码:
SELECT
Committee,
COUNT(CASE WHEN status = 'Resume Review' THEN 1 END) AS "Resume Review",
COUNT(CASE WHEN status = 'Interviewing' THEN 1 END) AS "Interviewing",
COUNT(CASE WHEN status = 'Coding Challenge' THEN 1 END) AS "Coding Challenge",
COUNT(*) AS Total
FROM EMPLOYEES
WHERE status IN ('Resume Review', 'Interviewing', 'Coding Challenge')
GROUP BY Committee;
which gives this:这给了这个:
Committee Resume Review Interviewing Take Home Challenge Total
UI/UX 3 2 1 6
Finance 0 2 2 4
Marketing 2 4 1 7
I am trying to achieve this:我正在努力实现这一目标:
Committee Resume Review Interviewing Take Home Challenge Total
UI/UX 3 2 1 6
Finance 0 2 2 4
Marketing 2 4 1 7
Total 5 8 4 17
Thanks in advance: :)提前致谢: :)
You can add 'ROLLUP' to 'GROUP BY'您可以将“ROLLUP”添加到“GROUP BY”
SELECT
CASE WHEN GROUPING(Committee) = 1 THEN 'Total' ELSE Committee END AS Committee,
COUNT(CASE WHEN status = 'Resume Review' THEN 1 END) AS "Resume Review",
COUNT(CASE WHEN status = 'Interviewing' THEN 1 END) AS "Interviewing",
COUNT(CASE WHEN status = 'Coding Challenge' THEN 1 END) AS "Coding Challenge",
COUNT(*) AS Total
FROM EMPLOYEES
WHERE status IN ('Resume Review', 'Interviewing', 'Coding Challenge')
GROUP BY Committee WITH ROLLUP
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.