SELECT
product_name,
sum_quantity total_sales,
rank() OVER(PARTITION BY category ORDER BY sum_quantity desc,product_id ) category_rank
FROM
(SELECT
o.product_id,
max(name) product_name,
max(category) category,
sum(quantity) sum_quantity
FROM products p
INNER JOIN orders o ON p.product_id = o.product_id
GROUP BY o.product_id) a
order by category,total_sales desc
- 关联订单信息表
- 求每个产品的销售总数
- 使用窗口函数根据销售总数排序

京公网安备 11010502036488号