1596. 每位顾客最经常订购的商品
写一个 SQL 语句,找到每一个顾客最经常订购的商品。
结果表单应该有每一位至少下过一次单的顾客 customer_id , 他最经常订购的商品的 product_id 和 product_name。
返回结果 没有顺序要求。
查询结果格式如下例所示。
select
t.customer_id,
t.product_id,
t.product_name
from
(SELECT
o.customer_id,
o.product_id,
p.product_name,
rank() over ( PARTITION BY customer_id ORDER BY count( 1 ) DESC ) rk
FROM
Orders as o join Products as p on p.product_id = o.product_id
GROUP BY
o.customer_id,
o.product_id) as t
where
t.rk = 1