SELECT c.name category,COUNT(DISTINCT p.id) assortment_size,SUM(tsl.quantity) total_sales,SUM(tsl.quantity)/COUNT(DISTINCT p.id) sales_per_product FROM categories c JOIN products p ON c.id=p.category_id JOIN transaction_sell_lines tsl ON p.id=tsl.product_id JOIN transactions t ON tsl.transaction_id=t.id WHERE t.type="sell" GROUP BY c.id,c.name ORDER BY sales_per_product DESC;