select product_id ,product_name ,category_id ,sales_amount ,profit_rate from (select t.product_id ,t.product_name ,t.category_id ,sum(t1.sales_amount) as sales_amount ,round((t1.sales_amount-t1.cost_amount)/t1.sales_amount,2) as profit_rate ,row_number()over(partition by t.category_id order by sum(t1.sales_amount) desc) as rn from product_category t left join sales_and_profit t1 on t.product_id=t1.product_id group by t.product_id ,t.product_name ,t.category_id) t where rn<=3 and profit_rate>0.2 order by t.category_id



京公网安备 11010502036488号