[개발관련]/Database(Mysql)

쿼리 연습 ex )세로축은 product_name별, 가로축은 주문년도를 "2015년", "2016년"으로 구성하여 product_name별, 년도별 주문건수를 집계하세요. (주문건수가 없으면 0으로 표시)

도담빠 2019. 4. 7. 16:20
반응형
select
product_name,
ifnull(sum(if(date_format(order_date, '%Y') = '2015', cnt, 0)),0) as "2015",
ifnull(sum(if(date_format(order_date, '%Y') = '2016', cnt, 0)),0) as "2016"
from product a join product_order b
on a.product_id = b.product_id
group by product_name

==

select
product_name,
ifnull(sum(if(date_format(order_date, '%Y') = '2015', cnt, 0)),0) as "2015",
ifnull(sum(if(date_format(order_date, '%Y') = '2016', cnt, 0)),0) as "2016"
from product a, product_order b
where a.product_id = b.product_id
group by product_name

 

728x90