with price_rk as (
    select product_id,
           rank() over(partition by type order by price desc) as rk
    from product_info
    group by type, product_id
)
select product_info.product_id, product_name, type, price
from product_info
join price_rk on product_info.product_id = price_rk.product_id
where rk <= 2
order by product_info.price desc, product_name
limit 3

题意是选取各商品种类中价格前2的商品,对这些商品再进行排序,最后取出该排序的前3名。

先使用窗口函数求出排名,注意使用rank(),价格相同时使用同一个排名