8. 온라인 쇼핑몰의 월 별 매출액 집계

2024-12-19

SELECT
substr(o.order_date, 1, 7) order_month
, SUM(price * quantity)FILTER(WHERE oi.order_id NOT LIKE 'C%') ordered_amount
, SUM(price * quantity)FILTER(WHERE oi.order_id LIKE 'C%') canceled_amount
, SUM(price * quantity) total_amount
FROM orders o
INNER JOIN order_items oi ON o.order_id = oi.order_id
GROUP BY order_month
ORDER BY order_month ASC

호기롭게 to_char() 쓰려다가 막혀서 오잉........ 했던 사건. postgresql에는 ... date type일때에 바로 substring을 사용할 수가 없다 .... 만약에 사용하고 싶으면 타입변환을 한번 해서 잘라줘야함

substring(o.order_date::varchar, 1, 7)
from orders

이런식으로!