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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T17:00:59+00:00 2026-05-12T17:00:59+00:00

I have two tables – let’s call them dbo.ValuesToReduce and dbo.Reserve The data in

  • 0

I have two tables – let’s call them dbo.ValuesToReduce and dbo.Reserve
The data in the first table (dbo.ValuesToReduce) is:

ValuesToReduceId | PartnerId | Value
-------------------------------------
1                | 1         | 53.15
2                | 2         | 601.98
3                | 1         | 91.05
4                | 2         | 44.56
5                | 3         | 19.11

The second table (dbo.Reserve) looks like this

ReserveId | PartnerId | Value
-------------------------------
1         | 1         | -101.55
2         | 2         | -425.19
3         | 3         | -28.17

What I need to do is: update the Values in ValuesToReduce table using the latter table of Reserves, reducing the numbers until the reserve supply is exhausted. Here’s what I should get after running the script:

ValuesToReduceId | PartnerId | Value
-------------------------------------
1                | 1         | 0.00
2                | 2         | 176.79
3                | 1         | 42.65
4                | 2         | 44.56
5                | 3         | 0.00


ReserveId | PartnerId | Value
-------------------------------
1         | 1         | 0.00
2         | 2         | 0.00
3         | 3         | -9.06

So basically, every partner has a “reserve” which he can deplete, and values in the value table should be reduced by partner accordingly if there is still something in the reserves. Reserves should be collocated in the order provided by ValuesToReduceId.

For partner with PartnerId of 1, you can see that he had enough reserve to update his first value to 0 and still had some left to reduce the second value by that amount.

Partner with ID of 2 had a reserve of 425.19, and there were two entries in the values table for that partner, 601.98 and 44.56, in that order (by ValuesToReduceId), so we only updated the first value since the reserve is not big enough for both. The wrong way would have been to update the second value to 0.00 and the first to 221.35.

Partner with ID of 3 has more than enough reserve, so after updating his value to 0, he’s left with -9.06

I tried something with recursive cte, but I can’t seem to get my head around it.
Hope I described the problem clearly enough..

  • 1 1 Answer
  • 2 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-12T17:00:59+00:00Added an answer on May 12, 2026 at 5:00 pm

    You cannot, as far as I know, update two tables in a single select statement.

    But you could do this in SQL using a WHILE loop. Search for the first transaction, then carry it out, until there are no possible transactions left.

    declare @valid int
    declare @resid int
    declare @val float
    while 1 = 1
        begin
    
        select top 1 
          @resid = r.ReserveId
        , @valid = v.ValuesToReduceId
        , @val = CASE WHEN -r.Value > v.Value THEN v.Value ELSE -r.Value END
        from ValuesToReduce v
        inner join Reserves r on r.PartnerId = v.PartnerId
        where r.Value < 0 and v.Value > 0
        order by r.ReserveId
    
        if @@rowcount = 0
            break
    
        update ValuesToReduce 
        set Value = Value - @val 
        where ValuesToReduceId = @valid
    
        update Reserves 
        set Value = Value + @val 
        where ReserveId = @resid
        end
    

    Here’s code to create test tables:

    create table ValuesToReduce (
        ValuesToReduceId int,
        PartnerId int,
        Value float
    )
    
    insert into ValuesToReduce values (1,1,53.15)
    insert into ValuesToReduce values (2,2,601.98)
    insert into ValuesToReduce values (3,1,91.05)
    insert into ValuesToReduce values (4,2,44.56)
    insert into ValuesToReduce values (5,3,19.11)
    
    create table Reserves (
        ReserveId int,
        PartnerId int,
        Value float
    )
    
    insert into Reserves values  (1,1,-101.55)
    insert into Reserves values (2,2,-425.19)
    insert into Reserves values (3,3,-28.17)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two tables with multiple connections, for example: First table: Tbl_Flights, it has
I have two tables with the following definitions: CREATE TABLE [dbo].[Shows] ( [Id] UNIQUEIDENTIFIER
Have two tables with a linking table between them. USERS +-------+---------+ | userID| Username|
I have two tables sharing a common key 'itemID', the first table holds the
I have two tables: table a ida valuea 1 a 2 b 3 c
I have two tables one with ID and NAME table 1 ID | NAME
I have two tables user table user_id | name | 1 | peter |
I have two tables, a user table and a application table: User id username
I have two tables connected with one to many relationship. Parent Table is a
Have two tables say ABC and XYZ and contain one column which data will

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.