ソースコード
WITH BASE AS 
(
	SELECT
		SUBSTR(S.SALES_DATE, 1, 7) AS YM,
		SUM(SALES_AMT) AS AMT
	FROM
		SALES S
	WHERE
		S.UPDATED_NO IS NULL
	GROUP BY
		YM
)
SELECT
	'2023-01' AS YEAR_MONTH,
	SUM(CASE WHEN YM = '2023-01' THEN AMT ELSE 0 END) AS MONTH_AMT,
	SUM(CASE WHEN SUBSTR(YM, 1, 4) = '2023' THEN AMT ELSE 0 END) AS CUML_AMT,
	SUM(AMT) AS YEAR_MOVE_AMT
FROM
	BASE
WHERE
	YM BETWEEN '2022-02' AND '2023-01'
UNION
SELECT
	'2023-02' AS YEAR_MONTH,
	SUM(CASE WHEN YM = '2023-02' THEN AMT ELSE 0 END) AS MONTH_AMT,
	SUM(CASE WHEN SUBSTR(YM, 1, 4) = '2023' THEN AMT ELSE 0 END) AS CUML_AMT,
	SUM(AMT) AS YEAR_MOVE_AMT
FROM
	BASE
WHERE
	YM BETWEEN '2022-03' AND '2023-02'
UNION
SELECT
	'2023-03' AS YEAR_MONTH,
	SUM(CASE WHEN YM = '2023-03' THEN AMT ELSE 0 END) AS MONTH_AMT,
	SUM(CASE WHEN SUBSTR(YM, 1, 4) = '2023' THEN AMT ELSE 0 END) AS CUML_AMT,
	SUM(AMT) AS YEAR_MOVE_AMT
FROM
	BASE
WHERE
	YM BETWEEN '2022-04' AND '2023-03'
UNION
SELECT
	'2023-04' AS YEAR_MONTH,
	SUM(CASE WHEN YM = '2023-04' THEN AMT ELSE 0 END) AS MONTH_AMT,
	SUM(CASE WHEN SUBSTR(YM, 1, 4) = '2023' THEN AMT ELSE 0 END) AS CUML_AMT,
	SUM(AMT) AS YEAR_MOVE_AMT
FROM
	BASE
WHERE
	YM BETWEEN '2022-05' AND '2023-04'
UNION
SELECT
	'2023-05' AS YEAR_MONTH,
	SUM(CASE WHEN YM = '2023-05' THEN AMT ELSE 0 END) AS MONTH_AMT,
	SUM(CASE WHEN SUBSTR(YM, 1, 4) = '2023' THEN AMT ELSE 0 END) AS CUML_AMT,
	SUM(AMT) AS YEAR_MOVE_AMT
FROM
	BASE
WHERE
	YM BETWEEN '2022-06' AND '2023-05'
UNION
SELECT
	'2023-06' AS YEAR_MONTH,
	SUM(CASE WHEN YM = '2023-06' THEN AMT ELSE 0 END) AS MONTH_AMT,
	SUM(CASE WHEN SUBSTR(YM, 1, 4) = '2023' THEN AMT ELSE 0 END) AS CUML_AMT,
	SUM(AMT) AS YEAR_MOVE_AMT
FROM
	BASE
WHERE
	YM BETWEEN '2022-07' AND '2023-06'
UNION
SELECT
	'2023-07' AS YEAR_MONTH,
	SUM(CASE WHEN YM = '2023-07' THEN AMT ELSE 0 END) AS MONTH_AMT,
	SUM(CASE WHEN SUBSTR(YM, 1, 4) = '2023' THEN AMT ELSE 0 END) AS CUML_AMT,
	SUM(AMT) AS YEAR_MOVE_AMT
FROM
	BASE
WHERE
	YM BETWEEN '2022-08' AND '2023-07'
UNION
SELECT
	'2023-08' AS YEAR_MONTH,
	SUM(CASE WHEN YM = '2023-08' THEN AMT ELSE 0 END) AS MONTH_AMT,
	SUM(CASE WHEN SUBSTR(YM, 1, 4) = '2023' THEN AMT ELSE 0 END) AS CUML_AMT,
	SUM(AMT) AS YEAR_MOVE_AMT
FROM
	BASE
WHERE
	YM BETWEEN '2022-09' AND '2023-08'
UNION
SELECT
	'2023-09' AS YEAR_MONTH,
	SUM(CASE WHEN YM = '2023-09' THEN AMT ELSE 0 END) AS MONTH_AMT,
	SUM(CASE WHEN SUBSTR(YM, 1, 4) = '2023' THEN AMT ELSE 0 END) AS CUML_AMT,
	SUM(AMT) AS YEAR_MOVE_AMT
FROM
	BASE
WHERE
	YM BETWEEN '2022-10' AND '2023-09'
UNION
SELECT
	'2023-10' AS YEAR_MONTH,
	SUM(CASE WHEN YM = '2023-10' THEN AMT ELSE 0 END) AS MONTH_AMT,
	SUM(CASE WHEN SUBSTR(YM, 1, 4) = '2023' THEN AMT ELSE 0 END) AS CUML_AMT,
	SUM(AMT) AS YEAR_MOVE_AMT
FROM
	BASE
WHERE
	YM BETWEEN '2022-11' AND '2023-10'
UNION
SELECT
	'2023-11' AS YEAR_MONTH,
	SUM(CASE WHEN YM = '2023-11' THEN AMT ELSE 0 END) AS MONTH_AMT,
	SUM(CASE WHEN SUBSTR(YM, 1, 4) = '2023' THEN AMT ELSE 0 END) AS CUML_AMT,
	SUM(AMT) AS YEAR_MOVE_AMT
FROM
	BASE
WHERE
	YM BETWEEN '2022-12' AND '2023-11'
UNION
SELECT
	'2023-12' AS YEAR_MONTH,
	SUM(CASE WHEN YM = '2023-12' THEN AMT ELSE 0 END) AS MONTH_AMT,
	SUM(CASE WHEN SUBSTR(YM, 1, 4) = '2023' THEN AMT ELSE 0 END) AS CUML_AMT,
	SUM(AMT) AS YEAR_MOVE_AMT
FROM
	BASE
WHERE
	YM BETWEEN '2023-01' AND '2023-12'


;
提出情報
提出日時2024/02/16 17:56:02
コンテスト第11回 SQLコンテスト
問題Zチャート
受験者tanaka
状態 (詳細)AC
(Accepted: 正答)
メモリ使用量94 MB
メッセージ
テストケース(通過数/総数)
2/2
状態
メモリ使用量
データパターン1
AC
94 MB
データパターン2
AC
84 MB