ソースコード
/*
都道府県毎に就学状況の人数を集計した就学状況データがある。都道府県、年齢別に就学先が横持ちで人数集計されているが、表示例のように都道府県、就学先で1レコードとなるように集計して縦持ちのデータに変換しなさい。 
また、集計対象とするのは、調査年が2020年のデータのみとし、KINDに表示する文言は、就学先の論理名とする。ただし、集計値がNULLのデータは表示対象としない。

表示項目は以下とする。(エイリアスを使用し→の項目名とする)

SURVEY_YEAR → SV_YEAR
PF_NAME → PREFECTURE
集計した就学先の種類 → KIND
就学先ごとに集計した人数 → AMT
表示順

都道府県コードの昇順
就学状況データの就学先を左から並べた順番(小学校→・・→大学院)
*/

with pre as
(
select survey_year,pf_code,1 as kind_cd,'小学校' as kind,sum(elementary) as amt from enrollment_Status where survey_year = 2020 and elementary is not null group by survey_year,pf_code  
union all
select survey_year,pf_code,2 as kind_cd,'中学校' as kind,sum(middle) as amt from enrollment_Status where survey_year = 2020 and middle is not null group by survey_year,pf_code
union all
select survey_year,pf_code,3 as kind_cd,'高校' as kind,sum(high) as amt from enrollment_Status where survey_year = 2020 and high is not null group by survey_year,pf_code
union all
select survey_year,pf_code,4 as kind_cd,'短大' as kind,sum(junior_clg) as amt from enrollment_Status where survey_year = 2020 and junior_clg is not null group by survey_year,pf_code
union all
select survey_year,pf_code,5 as kind_cd,'大学' as kind,sum(college) as amt from enrollment_Status where survey_year = 2020 and college is not null group by survey_year,pf_code
union all
select survey_year,pf_code,6 as kind_cd,'大学院' as kind,sum(graduate) as amt from enrollment_Status where survey_year = 2020 and graduate is not null group by survey_year,pf_code
)
select
    SURVEY_YEAR as SV_YEAR
    ,PF_NAME as PREFECTURE
    ,KIND
    ,AMT
from
    pre p
    left join
    prefecture pr
        on
        p.pf_code = pr.pf_code
order by
    p.pf_code
    ,kind_Cd
提出情報
提出日時2022/09/22 07:26:46
コンテスト第2回 SQLコンテスト
問題就学状況の表示変換
受験者sayaensm
状態 (詳細)WA
(Wrong Answer: 誤答)
メモリ使用量82 MB
メッセージ
テストケース(通過数/総数)
0/2
状態
メモリ使用量
データパターン1
WA
82 MB
データパターン2
WA
78 MB