SELECT HOUR(t.transaction_date) hour,p.name product,COUNT(t.id) transactions,SUM(tsl.quantity) units FROM transactions t JOIN transaction_sell_lines tsl ON t.id=tsl.transaction_id JOIN products p ON tsl.product_id=p.id WHERE t.type="sell" GROUP BY HOUR(t.transaction_date),p.id,p.name ORDER BY units DESC;