ソースコード
WITH next_month AS
(
SELECT
    MONTHLY,
    NEW_MRR,EXPANSION_MRR,DOWNGRADE_MRR,CHURN_MRR
FROM
    MRR_DATA
UNION
SELECT
    date(MAX(MONTHLY), '+1 month') AS MONTHLY,
    MAX(0) AS NEW_MRR,
    MAX(0) AS EXPANSION_MRR,
    MAX(0) AS DOWNGRADE_MRR,
    MAX(0) AS CHURN_MRR 
FROM
    MRR_DATA
    
)
, monthly_lag_mrr AS
(
SELECT
    MONTHLY,
    COALESCE(LAG(NEW_MRR,1) OVER(ORDER BY MONTHLY),0) AS lag_new,
    COALESCE(LAG(EXPANSION_MRR,1) OVER(ORDER BY MONTHLY),0) AS lag_exp,
    COALESCE(LAG(DOWNGRADE_MRR,1) OVER(ORDER BY MONTHLY),0) AS lag_down,
    COALESCE(LAG(CHURN_MRR,1) OVER(ORDER BY MONTHLY),0) AS lag_churn
FROM
    next_month
)
    --MRR = 前月のMRR + 前月のNEW_MRR + 前月のEXPANSION_MRR - 前月のDOWNGRADE_MRR - 前月のCHURN_MRR
,cal AS
(
SELECT
    MONTHLY,
    lag_new + lag_exp - lag_down - lag_churn AS cal
FROM
    monthly_lag_mrr
)
SELECT
    MONTHLY AS YM,
    -- cal,
    SUM(cal) OVER(ORDER BY MONTHLY ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS MRR
FROM
    cal
ORDER BY
    YM
提出情報
提出日時2023/02/19 17:17:09
コンテスト第5回 SQLコンテスト
問題MRRの集計
受験者aminoacid34
状態 (詳細)AC
(Accepted: 正答)
メモリ使用量98 MB
メッセージ
テストケース(通過数/総数)
3/3
状態
メモリ使用量
データパターン1
AC
79 MB
データパターン2
AC
79 MB
データパターン3
AC
98 MB