-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy path01084-sales-analysis-iii.sql
More file actions
27 lines (23 loc) · 939 Bytes
/
01084-sales-analysis-iii.sql
File metadata and controls
27 lines (23 loc) · 939 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
-- good one
-- when grouped by, min date should be in 1st quarter AND max date should be in first quarter
-- if max date is in another quarter, then do not o/p
-- use HAVING clause for these conditions
select s.product_id, p.product_name
from Sales s
join Product p
using(product_id)
group by 1
having min(sale_date) >= '2019-01-01' and max(sale_date) <= '2019-03-31'
------------------------------------------------------------------------------------------------------------------------------------------------------------
-- when product is between the given dates then 0 else 1- when sum > 0 that means product was sold outside the criteria
-- only o/p those products where flag = 0
with cte as
(select product_id,
sum(case when sale_date between '2019-01-01' and '2019-03-31' then 0 else 1 end) as flag
from Sales s
group by 1)
select s.product_id, p.product_name
from cte s
join Product p
using(product_id)
where flag = 0