ソースコード
SELECT
	 P.PF_CODE AS 都道府県コード
	,P.PF_NAME AS 都道府県名
	,ONE.NATION_NAME AS '1位 国名'
	,ONE.AMT AS '1位 人数'
	,TWO.NATION_NAME AS '2位 国名'
	,TWO.AMT AS '2位 人数'
	,THR.NATION_NAME AS '3位 国名'
	,THR.AMT AS '3位 人数'
	,AL.AMT  AS '合計人数'
FROM
	(
		SELECT
			*
		FROM
			(
				SELECT
					 PF_CODE
					,N.NATION_CODE
					,N.NATION_NAME
					,SUM(AMT) AS AMT
					,ROW_NUMBER() OVER(PARTITION BY PF_CODE
						ORDER BY
							 SUM(AMT) DESC
							,N.NATION_CODE
					) AS RANK
				FROM
					FOREIGNER AS F
				INNER JOIN
					NATIONALITY AS N
				ON F.NATION_CODE = N.NATION_CODE
				WHERE
					N.NATION_CODE <> '113'
				GROUP BY
					 PF_CODE
					,N.NATION_CODE
					,N.NATION_NAME
			) AS A
		WHERE
			RANK = 1
	) AS ONE
INNER JOIN
	(
		SELECT
			*
		FROM
			(
				SELECT
					 PF_CODE
					,N.NATION_CODE
					,N.NATION_NAME
					,SUM(AMT) AS AMT
					,ROW_NUMBER() OVER(PARTITION BY PF_CODE
						ORDER BY
							 SUM(AMT) DESC
							,N.NATION_CODE
					) AS RANK
				FROM
					FOREIGNER AS F
				INNER JOIN
					NATIONALITY AS N
				ON F.NATION_CODE = N.NATION_CODE
				WHERE
					N.NATION_CODE <> '113'
				GROUP BY
					 PF_CODE
					,N.NATION_CODE
					,N.NATION_NAME
			) AS A
		WHERE
			RANK = 2
	) AS TWO
ON ONE.PF_CODE = TWO.PF_CODE
INNER JOIN
	(
		SELECT
			*
		FROM
			(
				SELECT
					 PF_CODE
					,N.NATION_CODE
					,N.NATION_NAME
					,SUM(AMT) AS AMT
					,ROW_NUMBER() OVER(PARTITION BY PF_CODE
						ORDER BY
							 SUM(AMT) DESC
							,N.NATION_CODE
					) AS RANK
				FROM
					FOREIGNER AS F
				INNER JOIN
					NATIONALITY AS N
				ON F.NATION_CODE = N.NATION_CODE
				WHERE
					N.NATION_CODE <> '113'
				GROUP BY
					 PF_CODE
					,N.NATION_CODE
					,N.NATION_NAME
			) AS A
		WHERE
			RANK = 3
	) AS THR
ON ONE.PF_CODE = THR.PF_CODE
INNER JOIN
	(
		SELECT
			*
		FROM
			(
				SELECT
					 PF_CODE
					,N.NATION_CODE
					,N.NATION_NAME
					,SUM(AMT) AS AMT
					,ROW_NUMBER() OVER(PARTITION BY PF_CODE
						ORDER BY
							 SUM(AMT) DESC
							,N.NATION_CODE
					) AS RANK
				FROM
					FOREIGNER AS F
				INNER JOIN
					NATIONALITY AS N
				ON F.NATION_CODE = N.NATION_CODE
				WHERE
					N.NATION_CODE <> '113'
				GROUP BY
					 PF_CODE
			) AS A
	) AS AL
ON ONE.PF_CODE = AL.PF_CODE
INNER JOIN
	PREFECTURE AS P
ON ONE.PF_CODE = P.PF_CODE
ORDER BY
	 合計人数 DESC
	,都道府県コード ASC
提出情報
提出日時2022/07/21 09:16:25
コンテスト第1回 SQLコンテスト
問題外国籍分布
受験者SI_Sumitomo
状態 (詳細)AC
(Accepted: 正答)
メモリ使用量80 MB
メッセージ
テストケース(通過数/総数)
2/2
状態
メモリ使用量
データパターン1
AC
80 MB
データパターン2
AC
78 MB