# 【要求】:根据上面这两个表格,查询在'2024-01-01' —'2024-12-31'销量最高的产品,包含的字段:产品 ID、产品名称、总销售额、总销量。如果存在销量一样的多个产品,都展示出来且按照产品ID升序排列,要求查询出来的表格的字段如下:
# product_id: 产品的唯一标识符。
# product_name: 产品的名称。
# total_sales_amount: 总销售额。
# total_sales_quantity: 总销量。
select product_id
,product_name
,total_sales_amount
,total_sales_quantity
from(
select p.product_id
,product_name
,sum(sales_amount) as total_sales_amount
,sum(sales_quantity) as total_sales_quantity
,rank() over(order by sum(sales_quantity) desc) as rankk 
from sales_records s
left join products p on s.product_id=p.product_id
where sales_date between '2024-01-01' and '2024-12-31'
group by p.product_id,product_name
) t
where rankk=1
order by t.product_id;