ソースコード
WITH basemonthdata AS ( --月次売上
SELECT
	SUBSTR(SALES_DATE,1,7) AS YEAR_MONTH
	,SUM(SALES_AMT) AS MONTH_AMT
FROM SALES
WHERE UPDATED_NO IS NULL
GROUP BY 1
)
,cuml AS (
SELECT
	t1.YEAR_MONTH
	,t1.MONTH_AMT
	,SUM(t2.MONTH_AMT) AS CUML_AMT
FROM basemonthdata AS t1
LEFT OUTER JOIN basemonthdata AS t2
	ON t1.YEAR_MONTH >= t2.YEAR_MONTH
		AND t2.YEAR_MONTH BETWEEN '2023-01' AND '2023-12'
WHERE t1.YEAR_MONTH BETWEEN '2023-01' AND '2023-12'
GROUP BY 1,2
)
,dateadd AS (
SELECT
	YEAR_MONTH AS oldYEAR_MONTH
	,YEAR_MONTH || '-01' AS YEAR_MONTH
	,MONTH_AMT
FROM basemonthdata
)
, MOVEamt AS (
SELECT
	t1.oldYEAR_MONTH
	,t1.MONTH_AMT
	,SUM(t2.MONTH_AMT) AS YEAR_MOVE_AMT
FROM dateadd AS t1
LEFT OUTER JOIN dateadd AS t2
	ON t2.YEAR_MONTH BETWEEN DATE(t1.YEAR_MONTH, '-11 months') AND t1.YEAR_MONTH
GROUP BY 1,2
)
SELECT
	t1.YEAR_MONTH
	,t1.MONTH_AMT
	,t1.CUML_AMT
	,t2.YEAR_MOVE_AMT
FROM cuml AS t1
LEFT OUTER JOIN MOVEamt AS t2
	ON t1.YEAR_MONTH = t2.oldYEAR_MONTH
WHERE t1.YEAR_MONTH BETWEEN '2023-01' AND '2023-12'
ORDER BY 1
提出情報
提出日時2024/02/16 20:14:51
コンテスト第11回 SQLコンテスト
問題Zチャート
受験者katsu
状態 (詳細)AC
(Accepted: 正答)
メモリ使用量85 MB
メッセージ
テストケース(通過数/総数)
2/2
状態
メモリ使用量
データパターン1
AC
84 MB
データパターン2
AC
85 MB