ソースコード
with tmp as (
select
    f.PF_CODE,
    p.PF_NAME,
    f.NATION_CODE,
    n.NATION_NAME,
    sum(f.AMT) AMT,
    dense_rank() over(partition by f.PF_CODE order by f.AMT desc, f.NATION_CODE) RANK
from FOREIGNER f
left join PREFECTURE p
    on f.PF_CODE = p.PF_CODE
left join NATIONALITY n
    on f.NATION_CODE = n.NATION_CODE
where
    f.NATION_CODE != '113'
group by 1, 3
order by 1, 5 desc
)
select
    PF_CODE '都道府県コード',
    PF_NAME '都道府県名',
    max(case when RANK = 1 then NATION_NAME END) '1位 国名',
    max(case when RANK = 1 then AMT END) '1位 人数',
    max(case when RANK = 2 then NATION_NAME END) '2位 国名',
    max(case when RANK = 2 then AMT END) '2位 人数',
    max(case when RANK = 3 then NATION_NAME END) '3位 国名',
    max(case when RANK = 3 then AMT END) '3位 人数',
    sum(AMT) '合計人数'
from tmp
group by 1
order by 9 desc, 1
提出情報
提出日時2022/07/20 20:31:57
コンテスト第1回 SQLコンテスト
問題外国籍分布
受験者net008
状態 (詳細)AC
(Accepted: 正答)
メモリ使用量103 MB
メッセージ
テストケース(通過数/総数)
2/2
状態
メモリ使用量
データパターン1
AC
103 MB
データパターン2
AC
101 MB