ソースコード
WITH
ELEMENTARY AS
(
    SELECT
        SURVEY_YEAR
    ,   PF_CODE
    ,   '小学校' AS KIND
    ,   SUM(ELEMENTARY) AS AMT
    ,   1 AS ORDER_KEY
    FROM
        ENROLLMENT_STATUS
    WHERE
        SURVEY_YEAR = 2020
    GROUP BY
        SURVEY_YEAR
    ,   PF_CODE
),
MIDDLE AS
(
    SELECT
        SURVEY_YEAR
    ,   PF_CODE
    ,   '中学校' AS KIND
    ,   SUM(MIDDLE) AS AMT
    ,   2 AS ORDER_KEY
    FROM
        ENROLLMENT_STATUS
    WHERE
        SURVEY_YEAR = 2020
    GROUP BY
        SURVEY_YEAR
    ,   PF_CODE
),
HIGH AS
(
    SELECT
        SURVEY_YEAR
    ,   PF_CODE
    ,   '高校' AS KIND
    ,   SUM(HIGH) AS AMT
    ,   3 AS ORDER_KEY
    FROM
        ENROLLMENT_STATUS
    WHERE
        SURVEY_YEAR = 2020
    GROUP BY
        SURVEY_YEAR
    ,   PF_CODE
),
JUNIOR_CLG AS
(
    SELECT
        SURVEY_YEAR
    ,   PF_CODE
    ,   '短大' AS KIND
    ,   SUM(JUNIOR_CLG) AS AMT
    ,   4 AS ORDER_KEY
    FROM
        ENROLLMENT_STATUS
    WHERE
        SURVEY_YEAR = 2020
    GROUP BY
        SURVEY_YEAR
    ,   PF_CODE
),
COLLEGE AS
(
    SELECT
        SURVEY_YEAR
    ,   PF_CODE
    ,   '大学' AS KIND
    ,   SUM(COLLEGE) AS AMT
    ,   5 AS ORDER_KEY
    FROM
        ENROLLMENT_STATUS
    WHERE
        SURVEY_YEAR = 2020
    GROUP BY
        SURVEY_YEAR
    ,   PF_CODE
),
GRADUATE AS
(
    SELECT
        SURVEY_YEAR
    ,   PF_CODE
    ,   '大学院' AS KIND
    ,   SUM(GRADUATE) AS AMT
    ,   5 AS ORDER_KEY
    FROM
        ENROLLMENT_STATUS
    WHERE
        SURVEY_YEAR = 2020
    GROUP BY
        SURVEY_YEAR
    ,   PF_CODE
),
TOTAL AS
(
    SELECT * FROM ELEMENTARY
    UNION ALL
    SELECT * FROM MIDDLE
    UNION ALL
    SELECT * FROM HIGH
    UNION ALL
    SELECT * FROM JUNIOR_CLG
    UNION ALL
    SELECT * FROM COLLEGE
    UNION ALL
    SELECT * FROM GRADUATE
)
SELECT 
    TOTAL.SURVEY_YEAR  AS SV_YEAR
,   P.PF_NAME               AS PREFECTURE
,   TOTAL.KIND AS KIND
,   TOTAL.AMT   AS AMT
FROM
    PREFECTURE P
INNER JOIN 
    TOTAL
    ON P.PF_CODE = TOTAL.PF_CODE
WHERE 
    TOTAL.AMT IS NOT NULL
ORDER BY
    P.PF_CODE ASC
,   TOTAL.ORDER_KEY ASC;
提出情報
提出日時2022/09/22 11:15:09
コンテスト第2回 SQLコンテスト
問題就学状況の表示変換
受験者zakk3456
状態 (詳細)AC
(Accepted: 正答)
メモリ使用量107 MB
メッセージ
テストケース(通過数/総数)
2/2
状態
メモリ使用量
データパターン1
AC
107 MB
データパターン2
AC
86 MB