ソースコード
WITH ordered AS (
	SELECT
		pf_code,
		pf_name,
		nation_code,
		nation_name,
		sum(amt) AS sum_amt
	FROM
		foreigner,
		prefecture USING (pf_code),
		nationality USING (nation_code)
	WHERE
		nation_code <> 113
	GROUP BY
		pf_code,
		nation_code
	ORDER BY
		sum(amt)
		DESC
)
SELECT
	pf_code AS "都道府県コード",
	pf_name AS "都道府県名",
	(
		SELECT
			nation_name
		FROM
			ordered
		WHERE
			ordered.pf_code = foreigner.pf_code
		LIMIT 1) AS "1位 国名",
	(
		SELECT
			sum_amt
		FROM
			ordered
		WHERE
			ordered.pf_code = foreigner.pf_code
		LIMIT 1) AS "1位 人数",
	(
		SELECT
			nation_name
		FROM
			ordered
		WHERE
			ordered.pf_code = foreigner.pf_code
		LIMIT 1,
		1) AS "2位 国名",
	(
		SELECT
			sum_amt
		FROM
			ordered
		WHERE
			ordered.pf_code = foreigner.pf_code
		LIMIT 1,
		1) AS "2位 人数",
	(
		SELECT
			nation_name
		FROM
			ordered
		WHERE
			ordered.pf_code = foreigner.pf_code
		LIMIT 2,
		1) AS "3位 国名",
	(
		SELECT
			sum_amt
		FROM
			ordered
		WHERE
			ordered.pf_code = foreigner.pf_code
		LIMIT 2,
		1) AS "3位 人数",
	(
		SELECT
			sum(sum_amt)
		FROM
			ordered
		WHERE
			ordered.pf_code = foreigner.pf_code
		LIMIT 3) AS "合計人数"
FROM
	foreigner,
	prefecture USING (pf_code), nationality USING (nation_code)
GROUP BY
	pf_code
ORDER BY
	"合計人数" DESC, pf_code
提出情報
提出日時2022/10/08 13:38:26
コンテスト第1回 SQLコンテスト
問題外国籍分布
受験者haragumi
状態 (詳細)AC
(Accepted: 正答)
メモリ使用量79 MB
メッセージ
テストケース(通過数/総数)
2/2
状態
メモリ使用量
データパターン1
AC
79 MB
データパターン2
AC
76 MB