Day 08. ์จ๋ผ์ธ ์ผํ๋ชฐ์ ์ ๋ณ ๋งค์ถ์ก ์ง๊ณ
https://solvesql.com/problems/shoppingmall-monthly-summary/
WITH T1 AS (
SELECT oi.order_id
, DATE_FORMAT(o.order_date, '%Y-%m') AS order_month
, oi.price * oi.quantity AS amount
FROM orders AS o
INNER JOIN order_items AS oi ON o.order_id = oi.order_id
), n_cancel AS (
SELECT order_month
, SUM(amount) AS 'ordered_amount'
FROM T1
WHERE order_id NOT LIKE '%C%'
GROUP BY order_month
), cancel AS (
SELECT order_month
, SUM(amount) AS 'canceled_amount'
FROM T1
WHERE order_id LIKE '%C%'
GROUP BY order_month
)
SELECT nc.order_month
, nc.ordered_amount
, c.canceled_amount
, nc.ordered_amount + c.canceled_amount AS total_amount
FROM n_cancel AS nc
INNER JOIN cancel AS c ON nc.order_month = c.order_month
ORDER BY order_month ASC
โ์นผ๋ผ์ ๋ํด์ total_amount ์ด ์์ฑ
โINNER JOIN์ ํตํด ๋ง๋ค์ด ๋ ํ
์ด๋ธ ๊ฒฐํฉ
+) WITH ์ : T1
โDATE_FORMAT() ์ฌ์ฉํด์ ์ฐ-์๋ก ํฌ๋งท ๋ฐ๊พธ๊ธฐ
โINNER JOIN ์ฌ์ฉํด์ ํ
์ด๋ธ ๊ฒฐํฉ
+) WITH ์ : n_cancel
โSUM() ์ง๊ณํจ์ ์ฌ์ฉํด์ ํฉ ๊ตฌํ๊ธฐ
โWHERE ์ ์ NOT LIKE ์ฌ์ฉํด์ C๊ฐ ํฌํจ๋์ง ์๊ฒ ํํฐ๋ง
โGROUP BY ์ ์ ์ฐ์ ์ฌ์ฉ
+) WITH ์ : cancel
โSUM() ์ง๊ณํจ์ ์ฌ์ฉํด์ ํฉ ๊ตฌํ๊ธฐ
โWHERE ์ ์ LIKE ์ฌ์ฉํด์ C๊ฐ ํฌํจ๋๊ฒ ํํฐ๋ง
โGROUP BY ์ ์ ์ฐ์ ์ฌ์ฉ