-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeetCode1596.sql
More file actions
40 lines (36 loc) · 987 Bytes
/
LeetCode1596.sql
File metadata and controls
40 lines (36 loc) · 987 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
28
29
30
31
32
33
34
35
36
37
38
39
40
-- 先统计次数,后对比次数找出次数最大的
with
cte as (
select o.customer_id, o.product_id, p.product_name, COUNT(*) counts
from Orders o
left join Products p on o.product_id = p.product_id
group by
o.customer_id,
o.product_id
)
select t1.customer_id, t1.product_id, t1.product_name
from cte t1
join cte t2 on t1.customer_id = t2.customer_id
group by
t1.customer_id,
t1.product_id
having
sum(t1.counts < t2.counts) = 0;
-- 使用窗口函数对count直接排名
with
t as (
select customer_id, product_id, rank() over (
partition by
customer_id
order by count(product_id) desc
) as rk
from Orders
group by
customer_id,
product_id
)
select t.customer_id, t.product_id, p.product_name
from t
left join Products p on t.product_id = p.product_id
where
rk = 1;