SELECT HOUR(t.transaction_date) hour,AVG(CASE WHEN tsl.line_discount_amount>0 THEN (tsl.unit_price_inc_tax-tsl.unit_price_before_discount)/tsl.unit_price_before_discount ELSE NULL END)*100 promo_margin,AVG(CASE WHEN tsl.line_discount_amount=0 THEN (tsl.unit_price_inc_tax-tsl.unit_price_before_discount)/tsl.unit_price_before_discount ELSE NULL END)*100 regular_margin FROM transactions t JOIN transaction_sell_lines tsl ON t.id=tsl.transaction_id WHERE t.type="sell" GROUP BY HOUR(t.transaction_date) ORDER BY hour;