Using Cross Apply Query with Multiple Records

Posted on 2011-10-10
Last Modified: 2012-08-13
I am still very new to CTE & CROSS APPLY. I have this code:

    code varchar(5),
    qty int,
    bin varchar(10),
    date datetime 

INSERT INTO @bin (code, qty, bin, date)
SELECT 'A','1','Bin2','10/5/2005' UNION ALL
SELECT 'A','3','Bin3','10/8/2005' UNION ALL
SELECT 'A','5','Bin6 ','11/1/2006' UNION ALL
SELECT 'A','7','Bin9 ','11/5/2006' UNION ALL
SELECT 'A','1','Bin12','11/21/2009' UNION ALL
SELECT 'A','3','Bin22','12/25/2010' UNION ALL
SELECT 'B','22','Bin3','10/8/2005' UNION ALL
SELECT 'B','16','Bin19','11/1/2006' 

DECLARE @code varchar(10)
DECLARE @qtyNeeded int

SET @code = 'A'
SET @qtyNeeded = 8

;WITH BinsByDate AS
    SELECT code, qty, bin, date, ROW_NUMBER() OVER (ORDER BY date, bin, code) AS RowNum
    FROM   @Bin
    WHERE  code = @code
SELECT b.code, b.qty, b.bin,, bt.RunningTotal
          CASE WHEN bt.RunningTotal <= @qtyNeeded THEN b.qty 
                   ELSE b.qty - (bt.RunningTotal - @qtyNeeded) 
          END AS AdjQty
            ( SELECT SUM(t.qty) as RunningTotal
              FROM   BinsByDate t
              WHERE  t.RowNum <= b.RowNum
            ) bt
WHERE  bt.RunningTotal - b.Qty <= @qtyNeeded 

Open in new window

If I have another table called OrderList, which specifies the code and quantity parameters for each order:

    id int,
    code varchar(5),
    qty int,

Where code corresponds to @code and qty corresponds to @qtyNeeded

Is there an easier way to join @OrderList to the query above without using a while loop or cursor?

Question by:Candidochris
    LVL 59

    Accepted Solution

    You can just JOIN in that data unless I am missing something - no need for loop or a cursor. Are you getting a specific issue when you INNER or LEFT OUTER JOIN @OrderList to the rest of the query. It would seem you can join on b.code since it equals @code and qty = @qtyNeeded.

    Author Closing Comment

    You're right, I had a mistake in my query.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Do You Know the 4 Main Threat Actor Types?

    Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

    When you hear the word proxy, you may become apprehensive. This article will help you to understand Proxy and when it is useful. Let's talk Proxy for SQL Server. (Not in terms of Internet access.) Typically, you'll run into this type of problem w…
    International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
    Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
    Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now