ソースコード
with DATE_TBL AS (
    SELECT
        '2022-01-01' AS CLDATE
    UNION
    ALL
    SELECT
        DATE (CLDATE, '+1 months')
    FROM
        DATE_TBL
    WHERE
        CLDATE < '2024-01-01'
),
t1 as(
    select
        substr(SALES_DATE, 1, 7) as YEAR_MONTH,
        SALES_AMT as AMT
    from
        SALES
    where
        UPDATED_NO is null
    order by
        YEAR_MONTH
),
t2 as(
    select
        substr(DATE_TBL.CLDATE, 1, 7) as YEAR_MONTH,
        ifnull(sum(t1.AMT), 0) as AMT
    from
        t1
        right outer join DATE_TBL
    where
        t1.YEAR_MONTH == substr(DATE_TBL.CLDATE, 1, 7)
    group by
        YEAR_MONTH
),
t3 as (
    select
        YEAR_MONTH,
        AMT,
        rank() over(
            order by
                YEAR_MONTH
        ) as RANK
    from
        t2
),
t4 as (
    select
        t.YEAR_MONTH as YEAR_MONTH,
        t.AMT as MONTH_AMT,
        sum(
            case
                when "2023-01" <= t3.YEAR_MONTH
                and t3.RANK <= t.RANK then t3.AMT
                else 0
            end
        ) as CUML_AMT,
        sum(
            case
                when (t.RANK -11) <= t3.RANK
                and t3.RANK <= t.RANK then t3.AMT
                else 0
            end
        ) as YEAR_MOVE_AMT
    from
        t3 as t,
        t3
    group by
        t.YEAR_MONTH
)
select
    *
from
    t4
where
    "2023-01" <= YEAR_MONTH
    and YEAR_MONTH <= "2023-12"
order by
    YEAR_MONTH
提出情報
提出日時2024/02/19 04:22:51
コンテスト第11回 SQLコンテスト
問題Zチャート
受験者shogo314
状態 (詳細)AC
(Accepted: 正答)
メモリ使用量85 MB
メッセージ
テストケース(通過数/総数)
2/2
状態
メモリ使用量
データパターン1
AC
85 MB
データパターン2
AC
85 MB