ソースコード
WITH T1 AS(
    SELECT
        SALES_DTL.ITEM_CODE AS CODE,
        ITEM.ITEM_NAME AS NAME,
        SUM(UNITPRICE * SALES_QTY) AS SAL_AMT,
        RANK() OVER(ORDER BY SUM(UNITPRICE * SALES_QTY) DESC) AS RNK
    FROM
        SALES_DTL
        INNER JOIN
            ITEM ON ITEM.ITEM_CODE = SALES_DTL.ITEM_CODE,
            SALES ON SALES.SALES_NO = SALES_DTL.SALES_NO
     WHERE
        SALES_DATE BETWEEN '2023-06-01' AND '2023-06-30'
    GROUP BY
        SALES_DTL.ITEM_CODE
    ORDER BY
        SAL_AMT DESC,
        CODE DESC
),
T2 AS(
    SELECT
        SUM(SAL_AMT) AS TOTAL_AMT
    FROM
        T1
),
T3 AS(
    SELECT
        RNK,
        SUM(SAL_AMT) AS SAL_AMT
    FROM
        T1
    GROUP BY
        RNK
),
T4 AS(
    SELECT
        *
    FROM
        T3
),
T5 AS(
    SELECT
        T3.RNK AS RNK,
        T4.RNK AS RNK_2,
        T4.SAL_AMT AS SAL_AMT
    FROM
        T3
        CROSS JOIN
            T4
),
T6 AS(
    SELECT
        RNK,
        SUM(SAL_AMT) AS CML_AMT
    FROM
        T5
    WHERE
        RNK >= RNK_2
    GROUP BY
        RNK
)
SELECT
    CODE,
    NAME,
    SAL_AMT,
    CML_AMT,
    ROUND(SAL_AMT * 100.0 / TOTAL_AMT, 1) || '%' AS SAL_COMP,
    ROUND(CML_AMT * 100.0 / TOTAL_AMT, 1) || '%' AS TTL_COMP,
    CASE
        WHEN ROUND(CML_AMT * 100.0 / TOTAL_AMT, 1) BETWEEN 0.0   AND 40.0 THEN 'A'
        WHEN ROUND(CML_AMT * 100.0 / TOTAL_AMT, 1) BETWEEN 40.1   AND 80.0 THEN 'B'
        ELSE 'C'
    END AS RANK
    FROM
        T1
        INNER JOIN
            T6 ON T6.RNK = T1.RNK
        CROSS JOIN
            T2
提出情報
提出日時2023/08/31 23:50:22
コンテスト第7回 SQLコンテスト
問題ABC分析
受験者tokusakurai
状態 (詳細)AC
(Accepted: 正答)
メモリ使用量77 MB
メッセージ
テストケース(通過数/総数)
3/3
状態
メモリ使用量
データパターン1
AC
76 MB
データパターン2
AC
77 MB
データパターン3
AC
77 MB