Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6704197
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T07:15:39+00:00 2026-05-26T07:15:39+00:00

I’ve been researching CTEs trying to determine if it’s possible to recursively update inventory

  • 0

I’ve been researching CTEs trying to determine if it’s possible to recursively update inventory quantity records with an order quantity until the order quantity is consumed.

Here are the tables and records:

CREATE TABLE [dbo].[myOrder](
  [Account] [float] NOT NULL,
  [Item] [float] NOT NULL,
  [Quantity] [float] NOT NULL
) ON [PRIMARY]

insert into dbo.myOrder values (12345, 1, 50)

CREATE TABLE [dbo].[myInventory](
  [ID] [int] IDENTITY(1,1) NOT NULL,
  [Account] [float] NOT NULL,
  [InvDate] [numeric](18, 0) NOT NULL,
  [Item] [float] NOT NULL,
  [Quantity] [float] NOT NULL,
  [QuantitySold] [float] NOT NULL
) ON [PRIMARY]

insert into dbo.myInventory values (12345, 111287, 1, 45, 40)
insert into dbo.myInventory values (12345, 111290, 1, 40, 0)
insert into dbo.myInventory values (12345, 111290, 1, 12, 0)
insert into dbo.myInventory values (12345, 111291, 1, 25, 0)

The record in the myOrder table indicates that an order is to be created for account 12345 for item #1, quantity 50:

Account Item Quantity 
------- ---- --------
12345   1    50

The inventory table shows that we have plenty of item #1 on hand for account 12345:

ID Account InvDate Item Quantity QuantitySold
-- ------- ------- ---- -------- ------------
1  12345   111287  1    45       40
2  12345   111290  1    40       0
3  12345   111290  1    12       0
4  12345   111291  1    25       0

The goal is to start plugging in the order quantity of 50 into the inventory records until all 50 are consumed. Inventory records are ordered by the value in the InvDate column. Record 1 has 5 remaining quantity (45 – 40 = 5), which would leave us with 45 more to consume for the order. Record 2 can consume 40. Record 3 can consume the last 5. When the query completes the inventory records would look like this:

ID Account InvDate Item Quantity QuantitySold
-- ------- ------- ---- -------- ------------
1  12345   111287  1    45       45
2  12345   111290  1    40       40
3  12345   111290  1    12       5
4  12345   111291  1    25       0

Note: The inventory table stores QuantitySold, not QuantityRemaining, so you have to do the math (Quantity – QuantitySold) to determine how much quantity remains per inventory record.

I’ve gotten almost nowhere with the CTE. I’ve found plenty of examples for doing selects where you have 2 parts to your CTE – an initialization part and the recursive part UNIONed together. I could write this with a cursor, but I think it’s possible to do with a CTE and I’d like to learn how.

If anyone can confirm this is possible with a CTE or explain how to set up the CTE, I’d appreciate it. Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T07:15:40+00:00Added an answer on May 26, 2026 at 7:15 am
    --@inserted table mimics inserted virtual table from AFTER INSERT triggers on [dbo].[myOrder] table
    DECLARE @inserted TABLE 
    (
      [Account] [float] NOT NULL,
      [Item] [float] NOT NULL,
      [Quantity] [float] NOT NULL
    );
    
    INSERT  @inserted 
    VALUES  (12345, 1, 50);
    
    WITH CteRowNumber
    AS
    (
        SELECT   inv.ID
                ,inv.Account
                ,inv.Item
                ,inv.Quantity
                ,inv.QuantitySold
                ,i.Quantity QuantityOrdered
                ,ROW_NUMBER() OVER(PARTITION BY inv.Account,inv.Item ORDER BY inv.ID ASC) RowNumber
        FROM    myInventory inv
        INNER JOIN @inserted i ON inv.Account = i.Account 
        AND     inv.Item = i.Item 
        WHERE   inv.Quantity > inv.QuantitySold
    ),  CteRecursive
    AS
    (
        SELECT   a.ID
                ,a.Account
                ,a.Item
                ,a.RowNumber 
                ,CASE 
                    WHEN a.Quantity - a.QuantitySold < a.QuantityOrdered THEN a.Quantity - a.QuantitySold 
                    ELSE a.QuantityOrdered
                END QuantitySoldNew
                ,CASE 
                    WHEN a.Quantity - a.QuantitySold < a.QuantityOrdered THEN a.Quantity - a.QuantitySold 
                    ELSE a.QuantityOrdered
                END RunningTotal
        FROM    CteRowNumber a
        WHERE   a.RowNumber = 1
        UNION ALL
        SELECT   crt.ID
                ,crt.Account
                ,crt.Item
                ,crt.RowNumber
                ,CASE 
                    WHEN prev.RunningTotal + (crt.Quantity - crt.QuantitySold) < crt.QuantityOrdered THEN crt.Quantity - crt.QuantitySold
                    ELSE crt.QuantityOrdered - prev.RunningTotal
                END QuantitySoldNew
                ,CASE 
                    WHEN prev.RunningTotal + (crt.Quantity - crt.QuantitySold) < crt.QuantityOrdered THEN prev.RunningTotal + (crt.Quantity - crt.QuantitySold)
                    ELSE crt.QuantityOrdered
                END RunningTotal
        FROM    CteRecursive prev
        INNER JOIN CteRowNumber crt ON prev.Account = crt.Account 
        AND     prev.Item = crt.Item 
        AND     prev.RowNumber + 1 = crt.RowNumber
        WHERE   prev.RunningTotal  < crt.QuantityOrdered
    )
    SELECT   cte.ID
            ,cte.Account
            ,cte.Item
            ,cte.QuantitySoldNew
    FROM    CteRecursive cte;
    --or CteRecursive can be used to update QuantitySold column from [dbo].[myInventory] table
    --UPDATE    myInventory 
    --SET       QuantitySold = inv.QuantitySold + cte.QuantitySoldNew
    --FROM  myInventory inv
    --INNER JOIN CteRecursive cte ON inv.ID = cte.ID;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a text area in my form which accepts all possible characters from
I'm trying to select an H1 element which is the second-child in its group
I have been unable to fix a problem with Java Unicode and encoding. The

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.