select 
  product_id,
  product_name,
  total_sales_amount,
  total_sales_quantity
from (
  select 
    p.product_id product_id,
    max(product_name) product_name,
    sum(sales_amount) total_sales_amount,
    sum(sales_quantity) total_sales_quantity,
    rank() over (order by sum(sales_quantity) desc) rk
  from products p 
  join sales_records s on p.product_id = s.product_id
  where sales_date between '2024-01-01' and '2024-12-31'
  group by p.product_id
  ) t
where rk = 1

灵活使用窗口函数

窗口函数只用partition by,意思是只分组

只用order by 意思是不分组,只排序,并且orderby 后面可以使用聚合函数

限制排名,但输出的是该排名对应的列时,可以再子查询中给出排名和值,在主查询中用where筛选排名,进而输出排名对应的值,而不显示排名