簡體   English   中英

SQL中Pivot表如何創建總行和總列

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

我使用 SQL 創建了一個 pivot 表,但想要添加列和行總計。 我如何使用 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;

我當前的表:

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

所需表:

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

如果您願意不使用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;

用這個

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;

您可以使用條件聚合,然后使用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

或者,通過在條件聚合中包含所有列來獲取按列匯總:

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)

或者,使用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);

其中,對於示例數據:

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;

所有查詢output:

委員會 RESUME_REVIEW 回復 面試 編碼挑戰 雇用 全部的
金融 0 2個 2個 0 4個
營銷 2個 4個 1個 0 7
用戶界面/用戶體驗 3個 2個 1個 0 6個
全部的 5個 8個 4個 0 17

db<> 在這里擺弄

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM