ソースコード
WITH base1 AS ( -- データを横持ちにする
    SELECT
     ENROLLMENT_STATUS.SURVEY_YEAR AS SV_YEAR,
     PREFECTURE.PF_NAME AS PREFECTURE,
     PREFECTURE.PF_CODE AS PF_CODE,
     sum(ENROLLMENT_STATUS.ELEMENTARY) AS 小学校,
     sum(ENROLLMENT_STATUS.MIDDLE) AS 中学校,
     sum(ENROLLMENT_STATUS.HIGH) AS 高校,
     sum(ENROLLMENT_STATUS.JUNIOR_CLG) AS 短大,
     sum(ENROLLMENT_STATUS.COLLEGE) AS 大学,
     sum(ENROLLMENT_STATUS.GRADUATE) AS 大学院
    FROM
     ENROLLMENT_STATUS
    JOIN
     PREFECTURE
     on ENROLLMENT_STATUS.PF_CODE = PREFECTURE.PF_CODE
    WHERE
     ENROLLMENT_STATUS.SURVEY_YEAR = 2020
    GROUP BY
     ENROLLMENT_STATUS.SURVEY_YEAR, PREFECTURE.PF_NAME 
     
), base2 AS ( -- データを縦持ちにする
    SELECT
     SV_YEAR, PREFECTURE, '小学校' AS 学校区分, 小学校 AS AMT, 1 AS NUM, PF_CODE -- NUM, PF_CODE はソート用
    FROM 
     base1
    UNION ALL
    SELECT
     SV_YEAR, PREFECTURE, '中学校' AS 学校区分, 中学校 AS AMT, 2 AS NUM, PF_CODE
    FROM 
     base1
    UNION ALL
    SELECT
     SV_YEAR, PREFECTURE, '高校' AS 学校区分, 高校 AS AMT, 3 AS NUM, PF_CODE
    FROM 
     base1
    UNION ALL
    SELECT
     SV_YEAR, PREFECTURE, '短大' AS 学校区分, 短大 AS AMT, 4 AS NUM, PF_CODE
    FROM 
     base1
    UNION ALL
    SELECT
     SV_YEAR, PREFECTURE, '大学' AS 学校区分, 大学 AS AMT, 5 AS NUM, PF_CODE
    FROM 
     base1
    UNION ALL
    SELECT
     SV_YEAR, PREFECTURE, '大学院' AS 学校区分, 大学院 AS AMT, 6 AS NUM, PF_CODE
    FROM 
     base1
)

SELECT
 SV_YEAR, 
 PREFECTURE, 
 学校区分 AS KIND, 
 AMT
FROM 
 base2
WHERE 
 AMT IS NOT NULL
ORDER BY
 PF_CODE ASC, NUM ASC
 
提出情報
提出日時2022/09/22 04:22:04
コンテスト第2回 SQLコンテスト
問題就学状況の表示変換
受験者deep_ads
状態 (詳細)AC
(Accepted: 正答)
メモリ使用量79 MB
メッセージ
テストケース(通過数/総数)
2/2
状態
メモリ使用量
データパターン1
AC
79 MB
データパターン2
AC
79 MB