ソースコード
with i as (
  select PORT_CODE, KIND_CODE, SUM(AMT) as ammount
  from IMMIGRATION
  where GROUP_CODE = '120' -- 外国人
  and KIND_CODE in ('110', '120') -- '110'(入国者) 、'120'(出国者)
  group by PORT_CODE, KIND_CODE
)
, j as (
  select 
    PORT_CODE,
    max(CASE WHEN KIND_CODE = '110' THEN ammount END) as sum_in,
    max(CASE WHEN KIND_CODE = '120' THEN ammount END) as sum_out
  from i
  group by PORT_CODE
)
select PORT_CODE as "港コード",
PORT_NAME as "港名",
sum_in as "入国者数",
sum_out as "出国者数",
sum_in - sum_out as "差分"
from j 
inner join PORT using(PORT_CODE)
where sum_in > sum_out
order by (sum_in - sum_out) desc, PORT_CODE desc



提出情報
提出日時2022/07/20 21:29:26
コンテスト第1回 SQLコンテスト
問題港入出国者分析
受験者arze
状態 (詳細)AC
(Accepted: 正答)
メモリ使用量103 MB
メッセージ
テストケース(通過数/総数)
2/2
状態
メモリ使用量
データパターン1
AC
103 MB
データパターン2
AC
93 MB