SELECT HOUR(t.transaction_date) hour,COUNT(DISTINCT p.id) products_sold,SUM(tsl.quantity) units_sold,AVG(vld.qty_available) avg_inventory FROM transactions t JOIN transaction_sell_lines tsl ON t.id=tsl.transaction_id JOIN products p ON tsl.product_id=p.id LEFT JOIN variation_location_details vld ON p.id=vld.product_id WHERE t.type="sell" GROUP BY HOUR(t.transaction_date) ORDER BY units_sold/NULLIF(avg_inventory,0) DESC;