ソースコード
WITH p AS (
    SELECT
        *
    FROM
        PREFECTURE
    WHERE
        PF_NAME = '北海道' OR
        PF_NAME = '青森県' OR
        PF_NAME = '岩手県' OR
        PF_NAME = '宮城県' OR
        PF_NAME = '福島県'
),
sp_5 AS (
    SELECT
        s.AGE_CODE,
        SUM(s.SP_TIME_5) AS '5時間未満'
    FROM
        SLEEP_TIME_DTL AS s,
        p,
        AGE_GRP AS a
    WHERE
        s.PF_CODE = p.PF_CODE AND
        s.AGE_CODE = a.AGE_CODE
    GROUP BY
        s.AGE_CODE
),
sp_6 AS (
    SELECT
        s.AGE_CODE,
        SUM(s.SP_TIME_6) AS '5時間以上6時間未満'
    FROM
        SLEEP_TIME_DTL AS s,
        p,
        AGE_GRP AS a
    WHERE
        s.PF_CODE = p.PF_CODE AND
        s.AGE_CODE = a.AGE_CODE
    GROUP BY
        s.AGE_CODE
),
sp_7 AS (
    SELECT
        s.AGE_CODE,
        SUM(s.SP_TIME_7) AS '6時間以上7時間未満'
    FROM
        SLEEP_TIME_DTL AS s,
        p,
        AGE_GRP AS a
    WHERE
        s.PF_CODE = p.PF_CODE AND
        s.AGE_CODE = a.AGE_CODE
    GROUP BY
        s.AGE_CODE
),
sp_8 AS (
    SELECT
        s.AGE_CODE,
        SUM(s.SP_TIME_8) AS '7時間以上8時間未満'
    FROM
        SLEEP_TIME_DTL AS s,
        p,
        AGE_GRP AS a
    WHERE
        s.PF_CODE = p.PF_CODE AND
        s.AGE_CODE = a.AGE_CODE
    GROUP BY
        s.AGE_CODE
),
sp_9 AS (
    SELECT
        s.AGE_CODE,
        SUM(s.SP_TIME_9) AS '8時間以上9時間未満'
    FROM
        SLEEP_TIME_DTL AS s,
        p,
        AGE_GRP AS a
    WHERE
        s.PF_CODE = p.PF_CODE AND
        s.AGE_CODE = a.AGE_CODE
    GROUP BY
        s.AGE_CODE
),
sp_9o AS (
    SELECT
        s.AGE_CODE,
        SUM(s.SP_TIME_9OVER) AS '9時間以上'
    FROM
        SLEEP_TIME_DTL AS s,
        p,
        AGE_GRP AS a
    WHERE
        s.PF_CODE = p.PF_CODE AND
        s.AGE_CODE = a.AGE_CODE
    GROUP BY
        s.AGE_CODE
)

SELECT
    a.AGE_CODE AS 年齢コード,
    a.AGE_NAME AS 年齢階層名,
    sp_5.'5時間未満',
    sp_6.'5時間以上6時間未満',
    sp_7.'6時間以上7時間未満',
    sp_8.'7時間以上8時間未満',
    sp_9.'8時間以上9時間未満',
    sp_9o.'9時間以上'
FROM
    AGE_GRP AS a
LEFT JOIN sp_5
    ON a.AGE_CODE = sp_5.AGE_CODE
LEFT JOIN sp_6
    ON a.AGE_CODE = sp_6.AGE_CODE
LEFT JOIN sp_7
    ON a.AGE_CODE = sp_7.AGE_CODE
LEFT JOIN sp_8
    ON a.AGE_CODE = sp_8.AGE_CODE
LEFT JOIN sp_9
    ON a.AGE_CODE = sp_9.AGE_CODE
LEFT JOIN sp_9o
    ON a.AGE_CODE = sp_9o.AGE_CODE
ORDER BY
    年齢コード ASC
提出情報
提出日時2022/07/20 18:11:29
コンテスト第1回 SQLコンテスト
問題睡眠時間帯別集計
受験者money4_jam
状態 (詳細)AC
(Accepted: 正答)
メモリ使用量105 MB
メッセージ
テストケース(通過数/総数)
2/2
状態
メモリ使用量
データパターン1
AC
105 MB
データパターン2
AC
103 MB