ソースコード
WITH RECURSIVE
Months AS (
  SELECT 1 AS Month UNION ALL SELECT Month + 1 FROM Months WHERE Month < 12
),
MonthlySales AS (
  SELECT
    strftime('%Y-%m', SALES_DATE) AS YEAR_MONTH,
    SUM(SALES_AMT) AS MONTH_AMT
  FROM SALES
  WHERE UPDATED_NO IS NULL AND strftime('%Y', SALES_DATE) in ('2022', '2023')
  GROUP BY YEAR_MONTH
),
CumulativeSales AS (
  SELECT
    A.YEAR_MONTH,
    A.MONTH_AMT,
    (SELECT SUM(B.MONTH_AMT) FROM MonthlySales B WHERE B.YEAR_MONTH <= A.YEAR_MONTH and YEAR_MONTH like '%2023%') AS CUML_AMT
  FROM MonthlySales A
),
MovingYearTotal AS (
  SELECT
    A.YEAR_MONTH,
    A.MONTH_AMT,
    A.CUML_AMT,
    (SELECT SUM(B.MONTH_AMT) FROM MonthlySales B WHERE B.YEAR_MONTH BETWEEN 
     (SELECT CASE WHEN CAST(strftime('%m', A.YEAR_MONTH || '-01') AS INTEGER) - 11 < 1 
                  THEN strftime('%Y-%m', A.YEAR_MONTH || '-01', '-12 months') 
                  ELSE strftime('%Y-%m', A.YEAR_MONTH || '-01', '-11 months') END) AND A.YEAR_MONTH) AS YEAR_MOVE_AMT
  FROM CumulativeSales A
)
SELECT
  C.YEAR_MONTH,
  COALESCE(C.MONTH_AMT, 0) AS MONTH_AMT,
  COALESCE(C.CUML_AMT, 0) AS CUML_AMT,
  COALESCE(C.YEAR_MOVE_AMT, 0) AS YEAR_MOVE_AMT
FROM Months
LEFT JOIN (
  SELECT * FROM MovingYearTotal
) C ON C.YEAR_MONTH = '2023-' || (CASE WHEN Months.Month < 10 THEN '0' ELSE '' END || Months.Month)
ORDER BY C.YEAR_MONTH;
提出情報
提出日時2024/02/18 05:41:15
コンテスト第11回 SQLコンテスト
問題Zチャート
受験者hiraku
状態 (詳細)WA
(Wrong Answer: 誤答)
メモリ使用量85 MB
メッセージ
テストケース(通過数/総数)
0/2
状態
メモリ使用量
データパターン1
WA
84 MB
データパターン2
WA
85 MB