- Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy path_1596.sql
13 lines (12 loc) · 520 Bytes
/
_1596.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
--credit: https://leetcode.com/problems/the-most-frequently-ordered-products-for-each-customer/discuss/861257/simple-and-easy-solution-using-window-function
select customer_id, product_id, product_name from
(
selecto.customer_id, o.product_id, p.product_name,
rank() over (partition by customer_id order bycount(o.product_id) desc) as ranking
from Orders o
join Products p
ono.product_id=p.product_id
group by customer_id, product_id
) tmp
where ranking =1
order by customer_id, product_id