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
  1. 关联订单信息表
  2. 求每个产品的销售总数
  3. 使用窗口函数根据销售总数排序