with t1 as (select p.product_id as product_id,product_name,category_id,sales_amount,round((sales_amount-cost_amount)/sales_amount,2) as profit_rate,rank()over(partition by category_id order by sales_amount desc) as rank_sales from product_category p join sales_and_profit s on p.product_id = s.product_id) select product_id,product_name,category_id,sales_amount,profit_rate from t1 where rank_sales<=3 and profit_rate > 0.3

京公网安备 11010502036488号