SELECT HOUR(t.transaction_date) hour,COUNT(DISTINCT p.category_id) avg_categories,AVG(basket.total_items) avg_items FROM transactions t JOIN transaction_sell_lines tsl ON t.id=tsl.transaction_id JOIN products p ON tsl.product_id=p.id JOIN (SELECT transaction_id,SUM(quantity) total_items FROM transaction_sell_lines GROUP BY transaction_id) basket ON t.id=basket.transaction_id WHERE t.type="sell" GROUP BY HOUR(t.transaction_date) ORDER BY hour;