ソースコード
SELECT
    PF_CODE AS 都道府県コード,
    PF_NAME AS 都道府県名,
    MAX(CASE WHEN SUB.RANK = 1 THEN NATION_NAME ELSE NULL END) AS "1位 国名",
    MAX(CASE WHEN SUB.RANK = 1 THEN AMT ELSE 0 END) AS "1位 人数",
    MAX(CASE WHEN SUB.RANK = 2 THEN NATION_NAME ELSE NULL END) AS "2位 国名",
    MAX(CASE WHEN SUB.RANK = 2 THEN AMT ELSE 0 END) AS "2位 人数",
    MAX(CASE WHEN SUB.RANK = 3 THEN NATION_NAME ELSE NULL END) AS "3位 国名",
    MAX(CASE WHEN SUB.RANK = 3 THEN AMT ELSE 0 END) AS "3位 人数",
    SUM(AMT) AS 合計人数 
FROM
    ( 
        SELECT
            F.PF_CODE AS PF_CODE,
            P.PF_NAME AS PF_NAME,
            N.NATION_NAME AS NATION_NAME,
            F.AMT AS AMT,
            RANK() OVER ( 
                PARTITION BY F.PF_CODE 
                ORDER BY F.AMT DESC, F.NATION_CODE
            ) AS RANK
        FROM
            FOREIGNER AS F
            INNER JOIN NATIONALITY AS N 
                ON N.NATION_CODE = F.NATION_CODE 
            INNER JOIN PREFECTURE AS P 
                ON P.PF_CODE = F.PF_CODE 
        WHERE
            F.NATION_CODE != '113'
    ) AS SUB 
GROUP BY
    SUB.PF_CODE, SUB.PF_NAME
ORDER BY
    合計人数 DESC,
    都道府県コード ASC;
提出情報
提出日時2024/07/29 16:09:42
コンテスト第1回 SQLコンテスト
問題外国籍分布
受験者DSCSHR
状態 (詳細)AC
(Accepted: 正答)
メモリ使用量89 MB
メッセージ
テストケース(通過数/総数)
2/2
状態
メモリ使用量
データパターン1
AC
89 MB
データパターン2
AC
89 MB