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