ソースコード
with TMP1 as (
select
    strftime('%Y-%m', sales_date) as YEAR_MONTH
    ,strftime('%Y', sales_date) as Y
    ,cast(strftime('%m', sales_date) as integer) as M
    ,sum(SALES_AMT) as MONTH_AMT
from 
    SALES
where
    UPDATED_NO is null
group by 1
)
, TMP2 as (
select *
    ,lag(MONTH_AMT) over (order by YEAR_MONTH) as PREV1
    ,lag(MONTH_AMT,2) over (order by YEAR_MONTH) as PREV2
    ,lag(MONTH_AMT,3) over (order by YEAR_MONTH) as PREV3
    ,lag(MONTH_AMT,4) over (order by YEAR_MONTH) as PREV4
    ,lag(MONTH_AMT,5) over (order by YEAR_MONTH) as PREV5
    ,lag(MONTH_AMT,6) over (order by YEAR_MONTH) as PREV6
    ,lag(MONTH_AMT,7) over (order by YEAR_MONTH) as PREV7
    ,lag(MONTH_AMT,8) over (order by YEAR_MONTH) as PREV8
    ,lag(MONTH_AMT,9) over (order by YEAR_MONTH) as PREV9
    ,lag(MONTH_AMT,10) over (order by YEAR_MONTH) as PREV10
    ,lag(MONTH_AMT,11) over (order by YEAR_MONTH) as PREV11
from TMP1
)
select
    YEAR_MONTH
    ,MONTH_AMT
    ,MONTH_AMT+PREV1*iif(M>1,1,0)
    +PREV2*iif(M>2,1,0)
    +PREV3*iif(M>3,1,0)
    +PREV4*iif(M>4,1,0)
    +PREV5*iif(M>5,1,0)
    +PREV6*iif(M>6,1,0)
    +PREV7*iif(M>7,1,0)
    +PREV8*iif(M>8,1,0)
    +PREV9*iif(M>9,1,0)
    +PREV10*iif(M>10,1,0)
    +PREV11*iif(M>11,1,0) as CUML_AMT
    ,MONTH_AMT+PREV1+PREV2+PREV3+PREV4+PREV5+PREV6+PREV7+PREV8
    +PREV9+PREV10+PREV11 as YEAR_MOVE_AMT
from 
    TMP2
where
    Y='2023'
order by 1
;
提出情報
提出日時2024/02/18 13:03:48
コンテスト第11回 SQLコンテスト
問題Zチャート
受験者1120011
状態 (詳細)AC
(Accepted: 正答)
メモリ使用量87 MB
メッセージ
テストケース(通過数/総数)
2/2
状態
メモリ使用量
データパターン1
AC
85 MB
データパターン2
AC
87 MB