-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeetCode1084.sql
More file actions
43 lines (43 loc) · 1.16 KB
/
LeetCode1084.sql
File metadata and controls
43 lines (43 loc) · 1.16 KB
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
-- NOTE: 需要整个表遍历两次
select DISTINCT
Sales.product_id,
Product.product_name
from Sales
left join Product on Sales.product_id = Product.product_id
where (
Sales.product_id in (
select product_id
from Sales
where
sale_date BETWEEN '2019-01-01' AND '2019-03-31'
)
)
AND (
Sales.product_id not in(
select product_id
from Sales
where
DATEDIFF(sale_date, '2019-01-01') < 0
or DATEDIFF(sale_date, '2019-03-31') > 0
)
);
-- 分组在区间内的次数是否等于总次数
select sales.product_id as product_id, product.product_name as product_name
from sales
left join product on sales.product_id = product.product_id
group by
product_id
having
count(
sale_date between '2019-01-01' and '2019-03-31'
or null
) = count(*);
-- 筛选存在的日期范围
SELECT s.product_id, p.product_name
FROM Sales AS s
LEFT JOIN Product AS p ON s.product_id = p.product_id
GROUP BY
s.product_id
HAVING
MIN(sale_date) >= '2019-01-01'
AND MAX(sale_date) <= '2019-03-31'