learning/SQL

[LeetCode 1084] Sales Analysis III

유자유자 2023. 1. 31. 12:29

2019년 1 분기에만 팔린 제품들을 알아보자.

join 해서 날짜 필터링 해주면 된다. distinct 안 넣었다가 틀렸다. 

select distinct(p.product_id), (p.product_name)
from Product as p join 
    sales as s on p.product_id = s.product_id
where s.product_id not in (select distinct(product_id) 
                        from sales 
                        where sale_date < date("2019-01-01") or 
                            sale_date > date("2019-03-31"))
    and 
     s.sale_date >= date("2019-01-01") and 
    s.sale_date <= date("2019-03-31") ;