简体   繁体   English

SQL中Pivot表如何创建总行和总列

[英]How To Create Total Row and Total Column to Pivot Table in SQL

I have created a pivot table using SQL but want to add column and row totals.我使用 SQL 创建了一个 pivot 表,但想要添加列和行总计。 How am I able to achieve this using SQL. This is the following code I have for my pivot table.我如何使用 SQL 实现此目的。这是我的 pivot 表的以下代码。

SELECT * from 
(
    select committee, status, id from EMPLOYEES
) piv

pivot (
count (id)
for status in ('Resume Review', 'Interviewing', 'Coding Challenge', 'Hired')
) pivot_table;

My Current Table:我当前的表:

Committee   Resume Review   Interviewing    Take Home Challenge  
UI/UX              3             2                  1              
Finance            0             2                  2              
Marketing          2             4                  1          

Desired Table:所需表:

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

If you are open to not using the PIVOT operator, we can use conditional aggregation to achieve your result:如果您愿意不使用PIVOT运算符,我们可以使用条件聚合来实现您的结果:

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;

use this用这个

SELECT * from 
(
    select committee, status, id from EMPLOYEES
) piv

pivot (
count (id)
for status in ('Resume Review', 'Interviewing', 'Coding Challenge', 'Hired')
) pivot_table;

union all 

SELECT 'Total' as committee,
sum(Resume Review) as Resume Review,
sum(Interviewing) as Interviewing,
sum(Coding Challenge) as Coding Challenge,
sum(Hired) as Hired,
sum(Resume Review) + sum(Interviewing) +sum(Coding Challenge) + sum(Hired) as Total 
from 
(
    select committee, status, id from EMPLOYEES
) piv

pivot (
count (id)
for status in ('Resume Review', 'Interviewing', 'Coding Challenge', 'Hired')
) pivot_table;

You can use conditional aggregation and then ROLLUP to get a total row and then add the column values together to get a total column:您可以使用条件聚合,然后使用ROLLUP获取总计行,然后将列值加在一起以获得总计列:

SELECT r.*,
       resume_review + interviewing + coding_challenge + hired AS total
FROM   (
  SELECT CASE GROUPING_ID(committee)
         WHEN 0
         THEN committee
         ELSE 'Total'
         END AS committee,
         COUNT(CASE status WHEN 'Resume Review'    THEN id END) AS resume_review,
         COUNT(CASE status WHEN 'Interviewing'     THEN id END) AS interviewing,
         COUNT(CASE status WHEN 'Coding Challenge' THEN id END) AS coding_challenge,
         COUNT(CASE status WHEN 'Hired'            THEN id END) AS hired
  FROM   EMPLOYEES
  GROUP BY ROLLUP(committee)
) r

Or, by including all the columns in a conditional aggregation to get the column-wise totals:或者,通过在条件聚合中包含所有列来获取按列汇总:

SELECT CASE GROUPING_ID(committee)
       WHEN 0
       THEN committee
       ELSE 'Total'
       END AS committee,
       COUNT(CASE status WHEN 'Resume Review'    THEN id END) AS resume_review,
       COUNT(CASE status WHEN 'Interviewing'     THEN id END) AS interviewing,
       COUNT(CASE status WHEN 'Coding Challenge' THEN id END) AS coding_challenge,
       COUNT(CASE status WHEN 'Hired'            THEN id END) AS hired,
       COUNT(
         CASE
         WHEN status IN (
                'Resume Review', 'Interviewing', 'Coding Challenge', 'Hired'
              )
         THEN id
         END
       ) AS total
FROM   EMPLOYEES
GROUP BY ROLLUP(committee)

Or, using PIVOT and then aggregating and using ROLLUP :或者,使用PIVOT然后聚合并使用ROLLUP

SELECT CASE GROUPING_ID(committee)
       WHEN 0
       THEN committee
       ELSE 'Total'
       END AS committee,
       SUM(resume_review) AS resume_review,
       SUM(interviewing) AS interviewing,
       SUM(coding_challenge) AS coding_challenge,
       SUM(hired) AS hired,
       SUM(resume_review + interviewing + coding_challenge + hired) AS total
FROM   (SELECT committee, status, id FROM EMPLOYEES)
PIVOT  (
  COUNT(id)
  FOR status IN (
    'Resume Review'    AS resume_review,
    'Interviewing'     AS interviewing,
    'Coding Challenge' AS coding_challenge,
    'Hired'            AS hired
  )
)
GROUP BY ROLLUP(committee);

Which, for the sample data:其中,对于示例数据:

CREATE TABLE employees (id PRIMARY KEY, committee, status) AS
SELECT LEVEL +  0, 'UI/UX',     'Resume Review'    FROM DUAL CONNECT BY LEVEL <= 3 UNION ALL
SELECT LEVEL +  3, 'UI/UX',     'Interviewing'     FROM DUAL CONNECT BY LEVEL <= 2 UNION ALL
SELECT LEVEL +  5, 'UI/UX',     'Coding Challenge' FROM DUAL CONNECT BY LEVEL <= 1 UNION ALL
SELECT LEVEL +  6, 'Finance',   'Interviewing'     FROM DUAL CONNECT BY LEVEL <= 2 UNION ALL
SELECT LEVEL +  8, 'Finance',   'Coding Challenge' FROM DUAL CONNECT BY LEVEL <= 2 UNION ALL
SELECT LEVEL + 10, 'Marketing', 'Resume Review'    FROM DUAL CONNECT BY LEVEL <= 2 UNION ALL
SELECT LEVEL + 12, 'Marketing', 'Interviewing'     FROM DUAL CONNECT BY LEVEL <= 4 UNION ALL
SELECT LEVEL + 16, 'Marketing', 'Coding Challenge' FROM DUAL CONNECT BY LEVEL <= 1;

All the queries output:所有查询output:

COMMITTEE委员会 RESUME_REVIEW RESUME_REVIEW 回复 INTERVIEWING面试 CODING_CHALLENGE编码挑战 HIRED雇用 TOTAL全部的
Finance金融 0 0 2 2个 2 2个 0 0 4 4个
Marketing营销 2 2个 4 4个 1 1个 0 0 7 7
UI/UX用户界面/用户体验 3 3个 2 2个 1 1个 0 0 6 6个
Total全部的 5 5个 8 8个 4 4个 0 0 17 17

db<>fiddle here db<> 在这里摆弄

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM