ソースコード
with
  a as (
    select
      pf_code,
      nation_code,
      rank() over (
        partition by
          pf_code
        order by
          amt desc,
          nation_code
      ) as rnk,
    amt
    from
      foreigner
    where
      nation_code <> '113'
  )
select
  a.PF_CODE as "都道府県コード",
  pre.PF_name as "都道府県名",
  max( case when rnk == 1 then nation_name else null end) as "1位 国名",
  max( case when rnk == 1 then amt else null end) as "1位 人数",
  max( case when rnk == 2 then nation_name else null end) as "2位 国名",
  max( case when rnk == 2 then amt else null end) as "2位 人数",
  max( case when rnk == 3 then nation_name else null end) as "3位 国名",
  max( case when rnk == 3 then amt else null end) as "3位 人数",
  sum(amt) as "合計人数"
from
  a
  join nationality na on a.nation_code = na.nation_code
  join prefecture pre on a.pf_code = pre.pf_code
group by
  a.pf_code
order by 9 desc,1

提出情報
提出日時2024/04/16 01:22:40
コンテスト第1回 SQLコンテスト
問題外国籍分布
受験者orekwys
状態 (詳細)AC
(Accepted: 正答)
メモリ使用量88 MB
メッセージ
テストケース(通過数/総数)
2/2
状態
メモリ使用量
データパターン1
AC
85 MB
データパターン2
AC
88 MB