SELECT b.name brand,COUNT(t.id) sales,SUM(tsl.quantity) units,SUM(tsl.unit_price_inc_tax*tsl.quantity) revenue FROM brands b JOIN products p ON b.id=p.brand_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 b.id,b.name ORDER BY revenue DESC;