SELECT HOUR(t.transaction_date) hour,COUNT(DISTINCT CASE WHEN tsl.line_discount_amount>0 THEN t.id END) promo_sales,SUM(CASE WHEN tsl.line_discount_amount>0 THEN tsl.quantity ELSE 0 END) promo_units,AVG(CASE WHEN tsl.line_discount_amount>0 THEN tsl.line_discount_amount END) avg_discount 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 promo_sales DESC;