SELECT QUARTER(t.transaction_date) quarter,COUNT(t.id) transactions,SUM(t.final_total) revenue,SUM((tsl.unit_price_inc_tax-tsl.unit_price_before_discount)*tsl.quantity) profit FROM transactions t JOIN transaction_sell_lines tsl ON t.id=tsl.transaction_id WHERE t.type="sell" GROUP BY QUARTER(t.transaction_date) ORDER BY profit DESC;