SELECT HOUR(t.transaction_date) hour,COUNT(DISTINCT CASE WHEN t.type="stock_adjustment" THEN t.id END) adjustments,SUM(CASE WHEN t.type="stock_adjustment" THEN tsl.quantity ELSE 0 END) adjustment_quantity FROM transactions t JOIN transaction_sell_lines tsl ON t.id=tsl.transaction_id GROUP BY HOUR(t.transaction_date) ORDER BY adjustment_quantity DESC;