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 1049335
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T16:35:58+00:00 2026-05-16T16:35:58+00:00

I have two temp tables, both populated by TSQL queries. Temp Table A containing

  • 0

I have two temp tables, both populated by TSQL queries.

Temp Table A containing a Location, an Item, and a total inventory count.

Temp Table B containing a Location, an Item, a Sales Order Number, and negative inventory count.

I would like to loop through each item in Table B and subtract the negative inventory count from table A where the location and item match. Once the inventory reaches <=0 in Table B, I want to output each order into a 3rd table which holds the Location, Item, and Sales Order Number.

EXAMPLE TABLE A

Item|Location|Inventory
1    A        10
2    B        20

EXAMPLE TABLE B

Order|Item|Location|QuanityUsed
ABC  |1   |A       |5
ZYX  |2   |B       |10
DEF  |1   |A       |6

DEF would be output into TABLE C because there is not enough inventory to fill the order after subtracting order ABC.

How can I accomplish this?

  • 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-16T16:35:59+00:00Added an answer on May 16, 2026 at 4:35 pm

    You could use aubquery to calculate a running total. In the outer query, you could specify that the running total must be greater than the total inventory:

    select  location
    ,       item
    ,       [Order]
    ,       TotalInventory
    ,       RunningTotal
    from    (
            select  [order].location
            ,       [order].item
            ,       [order].[Order]
            ,       (
                    select  SUM(inventory)
                    from    @a inv
                    where   inv.item = [order].item
                            and inv.location = [order].location
                    ) as TotalInventory
            ,       (
                    select  SUM(QuantityUsed)
                    from    @b prevorder
                    where   prevorder.item = [order].item
                            and prevorder.location = [order].location
                            and prevorder.[order] <= [order].[order]
                    ) as RunningTotal
            from    @b [order]
            ) as OrderExtended
    where   TotalInventory < RunningTotal
    

    Test data:

    declare @a table (item int, location char(1), inventory int)
    insert into @a select 1, 'A', 10
    union all select 2, 'B', 20
    declare @b table ([order] char(3), item int, location char(1), QuantityUsed int)
    insert into @b select 'ABC', 1, 'A', 5
    union all select 'ZYX', 2, 'B', 10
    union all select 'DEF', 1, 'A', 6
    

    This prints:

    location  item  Order  TotalInventory  RunningTotal
    A         1     DEF    10              11
    

    So order DEF causes location A to exceed its item 1 inventory by 10 - 11 = 1.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

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.