SELECT category, orders_cnt, buyers_cnt, items_qty, revenue, avg_order_value, rank() over ( order by revenue desc, orders_cnt desc, category ) as rank_by_revenue FROM ( SELECT category, count(distinct t1.order_id) as orders_cnt, count(distinct buyer_id) as buyers_cnt, sum(qty) as items_qty, sum(qty ...