buyer가 2019년에 주문한 건수를 세 보자.
2019년도 주문 건수를 count로 세고 ifnull로 없는 사람들을 0으로 채웠다.
select u.user_id as buyer_id , u.join_date, ifnull(o.orders_in_2019,0) as orders_in_2019
from users as u left join
(select buyer_id, count(*) as orders_in_2019
from Orders
where year(order_date) = 2019
group by buyer_id )
as o
on o.buyer_id = u.user_id ;
'learning > SQL' 카테고리의 다른 글
[LeetCode 607] Sales Person (0) | 2023.01.30 |
---|---|
[LeetCode 197] Rising Temperature (0) | 2023.01.30 |
[LeetCode 1393] Capital Gain/Loss (0) | 2023.01.29 |
[LeetCode 176] Second Highest Salary (0) | 2023.01.29 |
[LeetCode 1667] Fix Names in a Table (0) | 2023.01.28 |
댓글