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

  • Home
  • SEARCH
  • 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 9189001
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T20:08:48+00:00 2026-06-17T20:08:48+00:00

I use SQL Server 2012 and I have these tables: Table Tb1 ( ID

  • 0

I use SQL Server 2012 and I have these tables:

Table Tb1 (ID Int, CodeID Int, Value Int)

 ID     CodeID    Value
 ----------------------
 1      1         10
 2      1         14
 3      1         5
 4      1         25
 5      2         12
 6      2         17
 7      2         4
 8      2         10
 9      2         6

Table Tb2 (CodeID Int Value Int)

CodeID    Value
---------------
1         25
2         20

I want a query for get records from Tb1 that SUM(Tb1.Value) <= Tb2.Value

For example result for above records is :

Tb1.ID   Tb1.CodeID  Tb1.Value   UsedValue     Tb2.Value
--------------------------------------------------------
1         1          10          10            25
2         1          14          14            25
3         1          5           1             25
5         2          12          12            20
6         2          17          8             20

I use cursor for get above query. But my query, execute with low performance.
I want a query without cursor.

EDIT 1
UsedValue is Tb1.Value until sum of Tb1.Value was lowest than Tb2.Value.

  • 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-06-17T20:08:50+00:00Added an answer on June 17, 2026 at 8:08 pm

    Try this:

    ;WITH Subtotals
    AS
    (
      SELECT
        T1.Id, T1.CodeId, SUM(T2.Value) SubTotal  
      FROM Tb1 T1
        JOIN Tb1 T2
           ON T1.CodeId = T2.CodeId
             AND T1.Id >= T2.Id
      GROUP BY T1.Id, T1.CodeId
    )
    SELECT
        S.ID,
        S.CodeID,
        T1.Value,
        CASE WHEN T2.value >= S.Subtotal
             THEN T1.value
             ELSE T1.value - (S.Subtotal - T2.value)
        END UsedValue,
        T2.Value T2Value
    FROM Subtotals S
       JOIN Tb2 T2
          ON S.CodeId = T2.CodeId
       JOIN Tb1 T1
          ON S.Id = T1.Id
    WHERE T2.Value >= S.SubTotal - T1.Value
    

    See result on SQL FIDDLE

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

Sidebar

Related Questions

I use SQL Server 2008 R2 and have a table that I want no
I use SQL Server 2005. I have a simple logging table that my web
I use SQL Server 2008 and have a table with 5 char typed columns.
I have a ms sql 2012 server and I use this function to add/records:
I have an error message that occurs every time I use SQL Server 2012
We have a SQL server 2008 and one of the tables, say table A
I have a table Messages in SQL Server 2012. There are three columns in
In development we use SQL Server 2012, but some customers might have versions as
I use this code to connect to SQL Server 2012, but it does not
I use this code to select password field from the sql server 2012 db,

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.