ソースコード
WITH PIVOT AS ( 
    SELECT
        1 AS SEQ_NO 
    UNION ALL 
    SELECT
        SEQ_NO + 1 
    FROM
        PIVOT
    WHERE
        SEQ_NO < 6
) 

SELECT SUB.SV_YEAR, SUB.PREFECTURE, 
(CASE
WHEN SEQ_NO = 1 THEN "小学校"
WHEN SEQ_NO = 2 THEN "中学校"
WHEN SEQ_NO = 3 THEN "高校"
WHEN SEQ_NO = 4 THEN "短大"
WHEN SEQ_NO = 5 THEN "大学"
WHEN SEQ_NO = 6 THEN "大学院"
END) AS KIND,
(CASE
WHEN SEQ_NO = 1 THEN SUB.ELEMENTARY_AMT
WHEN SEQ_NO = 2 THEN SUB.MIDDLE_AMT
WHEN SEQ_NO = 3 THEN SUB.HIGH_AMT
WHEN SEQ_NO = 4 THEN SUB.JUNIOR_CLG_AMT
WHEN SEQ_NO = 5 THEN SUB.COLLEGE_AMT
WHEN SEQ_NO = 6 THEN SUB.GRADUATE
END) AS AMT
FROM (
    SELECT ES.SURVEY_YEAR AS SV_YEAR, P.PF_NAME AS PREFECTURE, P.PF_CODE AS PF_CODE,
    SUM(ES.ELEMENTARY) AS ELEMENTARY_AMT,
    SUM(ES.MIDDLE) AS MIDDLE_AMT,
    SUM(ES.HIGH) AS HIGH_AMT,
    SUM(ES.JUNIOR_CLG) AS JUNIOR_CLG_AMT,
    SUM(ES.COLLEGE) AS COLLEGE_AMT,
    SUM(ES.GRADUATE) AS GRADUATE
    FROM ENROLLMENT_STATUS AS ES JOIN PREFECTURE AS P USING(PF_CODE)
    WHERE ES.SURVEY_YEAR = 2020
    GROUP BY P.PF_CODE
) AS SUB
JOIN PIVOT AS P
WHERE AMT IS NOT NULL
ORDER BY SUB.PF_CODE, SEQ_NO;
提出情報
提出日時2022/12/10 12:01:10
コンテスト第2回 SQLコンテスト
問題就学状況の表示変換
受験者roaris
状態 (詳細)AC
(Accepted: 正答)
メモリ使用量79 MB
メッセージ
テストケース(通過数/総数)
2/2
状態
メモリ使用量
データパターン1
AC
79 MB
データパターン2
AC
79 MB