SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Help creating query the that returns running total by item


Help creating query the that returns running total by item

Author
Message
Ken Peck
Ken Peck
Forum Newbie
Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)

Group: General Forum Members
Points: 1 Visits: 7
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 QtyOnHand
10100 2014-03-03 00:00:00.000 66 92
10100 2014-03-04 00:00:00.000 66 92
10100 2014-03-05 00:00:00.000 110 92
10100 2014-03-06 00:00:00.000 16 92
10100 2014-03-07 00:00:00.000 66 92
10100 2014-03-10 00:00:00.000 49 92
10100 2014-03-11 00:00:00.000 35 92
10102 2014-02-28 00:00:00.000 1 4912
10102 2014-03-02 00:00:00.000 2 4912
10102 2014-03-03 00:00:00.000 168 4912
10102 2014-03-04 00:00:00.000 3849 4912
10102 2014-03-05 00:00:00.000 684 4912
10102 2014-03-06 00:00:00.000 1421 4912
10102 2014-03-07 00:00:00.000 1902 4912
10102 2014-03-10 00:00:00.000 632 4912
10102 2014-03-11 00:00:00.000 455 4912
10102 2014-03-13 00:00:00.000 525 4912
10102 2014-03-17 00:00:00.000 84 4912
10102 2014-03-27 00:00:00.000 1 4912
10104 2014-03-04 00:00:00.000 320 143
10104 2014-03-05 00:00:00.000 62 143
10104 2014-03-06 00:00:00.000 10 143
10104 2014-03-07 00:00:00.000 32 143



CREATE 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




Please Help!

Thank you
Ken
Jack Corbett
  Jack Corbett
SSCoach
SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)

Group: General Forum Members
Points: 18278 Visits: 14889
I didn't spend a lot of time looking at the details of your question, but I think the top 3 results of this search on SSC will help you solve the problem. I particularly recommend the article by Jeff Moden.



Jack Corbett

Applications Developer

Don't let the good be the enemy of the best. -- Paul Fleming
At best you can say that one job may be more secure than another, but total job security is an illusion. -- Rod at work

Check out these links on how to get faster and more accurate answers:
Forum Etiquette: How to post data/code on a forum to get the best help
Need an Answer? Actually, No ... You Need a Question
How to Post Performance Problems
Crosstabs and Pivots or How to turn rows into columns Part 1
Crosstabs and Pivots or How to turn rows into columns Part 2
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search