with sub as( select p.product_id as product_id, p.product_name as product_name, sum(s.sales_amount) as total_sales_amount, sum(s.sales_quantity) as total_sales_quantity, dense_rank()over(order by sum(s.sales_quantity) desc) as rk from products p join sales_records s on p.product_id=s.product_id where s.sales_date between '2024-01-01' and '2024-12-31' group by p.product_id) select product_id,product_name,total_sales_amount,total_sales_quantity from sub where rk=1 order by product_id 【注意点:1、全局排序,不需要加“partition by”语句;2、dense_rank是存在相同排序-且不跳跃的排序函数,row_number是不存在相同排序-且不跳跃的排序函数,rank是存在相同且跳跃的排序函数。】