ソースコード
SELECT
    SUB.PF_CODE AS 都道府県コード
    , SUB.PF_NAME AS 都道府県名
    , MAX(CASE SUB.RANK WHEN 1 THEN NATION_NAME ELSE NULL END) AS "1位 国名"
    , MAX(CASE SUB.RANK WHEN 1 THEN AMT ELSE 0 END) AS "1位 人数"
    , MAX(CASE SUB.RANK WHEN 2 THEN NATION_NAME ELSE NULL END) AS "2位 国名"
    , MAX(CASE SUB.RANK WHEN 2 THEN AMT ELSE 0 END) AS "2位 人数"
    , MAX(CASE SUB.RANK WHEN 3 THEN NATION_NAME ELSE NULL END) AS "3位 国名"
    , MAX(CASE SUB.RANK WHEN 3 THEN AMT ELSE 0 END) AS "3位 人数"
    , SUB2.SUMMARY 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
            , ROW_NUMBER() OVER ( 
                PARTITION BY
                    F.PF_CODE 
                ORDER BY
                    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
    inner join
    ( 
        SELECT
            F.PF_CODE AS PF_CODE
            , SUM(F.AMT) AS SUMMARY
        FROM
            FOREIGNER AS F 
        GROUP BY
            F.PF_CODE
    ) AS SUB2 ON SUB.PF_CODE = SUB2.PF_CODE
GROUP BY
    SUB.PF_CODE 
    , SUB.PF_NAME
ORDER BY
    合計人数 DESC
    , 都道府県コード ASC;
 
提出情報
提出日時2022/07/26 19:31:51
コンテスト第1回 SQLコンテスト
問題外国籍分布
受験者udo
状態 (詳細)WA
(Wrong Answer: 誤答)
メモリ使用量80 MB
メッセージ
テストケース(通過数/総数)
0/2
状態
メモリ使用量
データパターン1
WA
80 MB
データパターン2
WA
77 MB