ソースコード
with foreigner_with_order as (
    select
        pf_code,
        nation_code,
        amt,
        rank () over (
            partition by pf_code
            order by
                amt desc
        ) as amt_order
    from
        foreigner
    where
        nation_code <> '113'
),
merge_all as (
    select
        f.pf_code,
        p.pf_name,
        n.nation_name,
        f.amt,
        f.amt_order
    from
        foreigner_with_order as f
        inner join nationality as n on f.nation_code = n.nation_code
        inner join prefecture as p on f.pf_code = p.pf_code
)
select
    sub.pf_code as "都道府県コード",
    sub.pf_name as "都道府県名",
    (
        select
            m.nation_name
        from
            merge_all as m
        where
            sub.pf_code = m.pf_code
            and m.amt_order = 1
    ) as "1位 国名",
    (
        select
            m.amt
        from
            merge_all as m
        where
            sub.pf_code = m.pf_code
            and m.amt_order = 1
    ) as "1位 人数",
    (
        select
            m.nation_name
        from
            merge_all as m
        where
            sub.pf_code = m.pf_code
            and m.amt_order = 2
    ) as "2位 国名",
    (
        select
            m.amt
        from
            merge_all as m
        where
            sub.pf_code = m.pf_code
            and m.amt_order = 2
    ) as "2位 人数",
    (
        select
            m.nation_name
        from
            merge_all as m
        where
            sub.pf_code = m.pf_code
            and m.amt_order = 3
    ) as "3位 国名",
    (
        select
            m.amt
        from
            merge_all as m
        where
            sub.pf_code = m.pf_code
            and m.amt_order = 3
    ) as "3位 人数",
    (
        select
            sum(m.amt)
        from
            merge_all as m
        group by
            m.pf_code
        having
            sub.pf_code = m.pf_code
    ) as "合計人数"
from
    (
        select
            distinct f.pf_code,
            p.pf_name
        from
            foreigner as f
            inner join prefecture as p on f.pf_code = p.pf_code
    ) as sub
order by
    "合計人数" desc,
    "都道府県コード" asc;
提出情報
提出日時2022/07/24 01:54:06
コンテスト第1回 SQLコンテスト
問題外国籍分布
受験者nnenn0
状態 (詳細)WA
(Wrong Answer: 誤答)
メモリ使用量80 MB
メッセージ
テストケース(通過数/総数)
1/2
状態
メモリ使用量
データパターン1
WA
80 MB
データパターン2
AC
78 MB