I'm trying to create a query that will roll through sales orders by item in date (PromiseDate)order. With a known inventory amount (QtyOnHand) that subtracts the QtyOrdered amount for each order and keeps a running total of the QtyOnHand after subtracting the QtyOrdered and only writes to my table when the QtyOnHand is < 1 that way I know the date e specific item will be out of inventory.After processing the follow data my new table should contain three records, one for each item representing the date that item would run out of inventory.Item PromiseDate QtyOrdered QtyOnHand10100 2014-03-03 00:00:00.000 66 9210100 2014-03-04 00:00:00.000 66 9210100 2014-03-05 00:00:00.000 110 9210100 2014-03-06 00:00:00.000 16 9210100 2014-03-07 00:00:00.000 66 9210100 2014-03-10 00:00:00.000 49 9210100 2014-03-11 00:00:00.000 35 9210102 2014-02-28 00:00:00.000 1 491210102 2014-03-02 00:00:00.000 2 491210102 2014-03-03 00:00:00.000 168 491210102 2014-03-04 00:00:00.000 3849 491210102 2014-03-05 00:00:00.000 684 491210102 2014-03-06 00:00:00.000 1421 491210102 2014-03-07 00:00:00.000 1902 491210102 2014-03-10 00:00:00.000 632 491210102 2014-03-11 00:00:00.000 455 491210102 2014-03-13 00:00:00.000 525 491210102 2014-03-17 00:00:00.000 84 491210102 2014-03-27 00:00:00.000 1 491210104 2014-03-04 00:00:00.000 320 14310104 2014-03-05 00:00:00.000 62 14310104 2014-03-06 00:00:00.000 10 14310104 2014-03-07 00:00:00.000 32 143Please Help if you can.Thank youKenCREATE TABLE #OverInv (Item VARCHAR(15), PromiseDate DATETIME, QtyOnHand INT, QtyOrdered INT, TotalOver INT) DECLARE @Item VARCHAR(15), @PromiseDate DATETIME, @QtyOrdered INT, @QtyOnHand INT, @CurrentItem VARCHAR(15), @TotalOver INT SET @TotalOver = 0 SET @CurrentItem = 0 DECLARE rt_cursor CURSOR FOR SELECT Item, PromiseDate, QtyOrdered, QtyOnHand FROM Inventory OPEN rt_cursor FETCH NEXT FROM rt_cursor INTO @Item,@PromiseDate,@QtyOrdered,@QtyOnHand WHILE @@FETCH_STATUS = 0 IF @CurrentItem = 0 BEGIN SET @CurrentItem = @Item SET @TotalOver = (@QtyOnHand - @QtyOrdered) IF @TotalOver < 1 BEGIN INSERT #OverInv VALUES (@Item,@PromiseDate,@QtyOnHand,@QtyOrdered,@TotalOver) FETCH NEXT FROM rt_cursor INTO @Item,@PromiseDate,@QtyOrdered,@QtyOnHand SET @TotalOver = 0 END ELSE BEGIN FETCH NEXT FROM rt_cursor INTO @Item,@PromiseDate,@QtyOrdered,@QtyOnHand END END IF @CurrentItem = @Item SET @TotalOver = (@TotalOver - @QtyOrdered) BEGIN IF @TotalOver < 1 BEGIN INSERT #OverInv VALUES (@Item,@PromiseDate,@QtyOnHand,@QtyOrdered,@TotalOver) FETCH NEXT FROM rt_cursor INTO @Item,@PromiseDate,@QtyOrdered,@QtyOnHand SET @TotalOver = 0 END ELSE BEGIN FETCH NEXT FROM rt_cursor INTO @Item,@PromiseDate,@QtyOrdered,@QtyOnHand END END IF @CurrentItem <> @Item SET @CurrentItem = @Item SET @TotalOver = (@QtyOnHand - @QtyOrdered) BEGIN IF @TotalOver < 1 BEGIN INSERT #OverInv VALUES (@Item,@PromiseDate,@QtyOnHand,@QtyOrdered,@TotalOver) FETCH NEXT FROM rt_cursor INTO @Item,@PromiseDate,@QtyOrdered,@QtyOnHand SET @TotalOver = 0 END ELSE BEGIN FETCH NEXT FROM rt_cursor INTO @Item,@PromiseDate,@QtyOrdered,@QtyOnHand END END CLOSE rt_cursor DEALLOCATE rt_cursor SELECT * FROM #OverInv ORDER BY Item DROP TABLE #OverInv