ソースコード
SELECT
	p.PF_CODE as 都道府県コード,
	p.PF_NAME as 都道府県名,
	(SELECT
		b.NATION_NAME
	FROM
		FOREIGNER a,
		NATIONALITY b
	WHERE
		a.PF_CODE = f.PF_CODE AND
		a.NATION_CODE = b.NATION_CODE AND
		a.NATION_CODE <> "113"
	ORDER BY a.AMT DESC
	LIMIT 1
	) as "1位 国名",
	(SELECT
		a.AMT
	FROM
		FOREIGNER a
	WHERE
		a.PF_CODE = f.PF_CODE AND
		a.NATION_CODE <> "113"
	ORDER BY a.AMT DESC
	LIMIT 1
	) as "1位 人数",
	(SELECT
		b.NATION_NAME
	FROM
		FOREIGNER a,
		NATIONALITY b
	WHERE
		a.PF_CODE = f.PF_CODE AND
		a.NATION_CODE = b.NATION_CODE AND
		a.NATION_CODE <> "113"
	ORDER BY a.AMT DESC
	LIMIT 1 OFFSET 1
	) as "2位 国名",
	(SELECT
		a.AMT
	FROM
		FOREIGNER a
	WHERE
		a.PF_CODE = f.PF_CODE AND
		a.NATION_CODE <> "113"
	ORDER BY a.AMT DESC
	LIMIT 1 OFFSET 1
	) as "2位 人数",
	(SELECT
		b.NATION_NAME
	FROM
		FOREIGNER a,
		NATIONALITY b
	WHERE
		a.PF_CODE = f.PF_CODE AND
		a.NATION_CODE = b.NATION_CODE AND
		a.NATION_CODE <> "113"
	ORDER BY a.AMT DESC
	LIMIT 1 OFFSET 2
	) as "3位 国名",
	(SELECT
		a.AMT
	FROM
		FOREIGNER a
	WHERE
		a.PF_CODE = f.PF_CODE AND
		a.NATION_CODE <> "113"
	ORDER BY a.AMT DESC
	LIMIT 1 OFFSET 2
	) as "3位 人数",
	f.TOTAL_AMT as 合計人数
FROM
	(SELECT
		PF_CODE,
		sum(AMT) as TOTAL_AMT
	FROM FOREIGNER
	WHERE
		NATION_CODE <> "113"
	GROUP BY PF_CODE
	) f,
	PREFECTURE p
WHERE
	f.PF_CODE = p.PF_CODE
ORDER BY
	合計人数 DESC,
	都道府県コード ASC
提出情報
提出日時2022/07/20 20:55:15
コンテスト第1回 SQLコンテスト
問題外国籍分布
受験者ks2m
状態 (詳細)AC
(Accepted: 正答)
メモリ使用量101 MB
メッセージ
テストケース(通過数/総数)
2/2
状態
メモリ使用量
データパターン1
AC
99 MB
データパターン2
AC
101 MB