注意ROW_NUMBER()排序里order by 的顺序 select pd.name as product_name, sum(quantity) as total_sales, row_number() over (partition by pd.category order by sum(quantity) desc,pd.product_id) as category_rank from orders as od join products as pd on od.product_id=pd.product_id group by pd.name,od.product_id order by pd.category,total_sales desc ;