ソースコード

WITH next_month AS
(
SELECT
    MONTHLY,
    NEW_MRR,EXPANSION_MRR,DOWNGRADE_MRR,CHURN_MRR
FROM
    MRR_DATA
UNION
SELECT
    '2023-01-01' AS MONTHLY,
    0 AS NEW_MRR,
    0 AS EXPANSION_MRR,
    0 AS DOWNGRADE_MRR,
    0 AS CHURN_MRR
    
)

, monthly_lag_mrr AS
(
SELECT
    -- YM,
    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,
    -- cal,
    SUM(cal) OVER(ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS MRR
FROM
    cal
提出情報
提出日時2023/02/19 16:50:59
コンテスト第5回 SQLコンテスト
問題MRRの集計
受験者aminoacid34
状態 (詳細)WA
(Wrong Answer: 誤答)
メモリ使用量99 MB
メッセージ
テストケース(通過数/総数)
0/3
状態
メモリ使用量
データパターン1
WA
81 MB
データパターン2
WA
99 MB
データパターン3
WA
94 MB