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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T19:04:26+00:00 2026-05-13T19:04:26+00:00

Table_1 D_ID Integer Deposit_amt integer Table_2 Total_ID Total_amt integer Is it possible to write

  • 0

Table_1
D_ID Integer
Deposit_amt integer

Table_2
Total_ID
Total_amt integer

Is it possible to write a select statement to find all the rows in Table_1 whose Deposit_amt sum to the Total_amt in Table_2. There are multiple rows in both tables.

Say the first row in Table_2 has a Total_amt=100. I would want to know that in Table_1 the rows with D_ID 2, 6, 12 summed = 100, the rows D_ID 2, 3, 42 summed = 100, etc.

Help appreciated. Let me know if I need to clarify.

I am asking this question as someone as part of their job has a list of transactions and a list of totals, she needs to find the possible list of transactions that could have created the total. I agree this sounds dangerous as finding a combination of transactions that sums to a total does not guarantee that they created the total.

I wasn’t aware it is an np-complete problem.

  • 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-13T19:04:26+00:00Added an answer on May 13, 2026 at 7:04 pm

    Just for fun I did a brute force solution. It will find combinations of one, two, or three records that add up to Total_amt. You could expand it to handle more transactions per sum, by adding d4, d5 subselects, etc.:

    begin tran
    
    create table Table_1 (D_ID int, Deposit_amt int)
    create table Table_2 (Total_ID int, Total_amt int)
    
    insert into Table_1 (D_ID, Deposit_amt) values (1, 4)
    insert into Table_1 (D_ID, Deposit_amt) values (2, 3)
    insert into Table_1 (D_ID, Deposit_amt) values (3, 1)
    insert into Table_1 (D_ID, Deposit_amt) values (4, 1)
    insert into Table_1 (D_ID, Deposit_amt) values (5, 9)
    insert into Table_1 (D_ID, Deposit_amt) values (6, 13)
    insert into Table_1 (D_ID, Deposit_amt) values (7, 6)
    insert into Table_1 (D_ID, Deposit_amt) values (8, 7)
    insert into Table_1 (D_ID, Deposit_amt) values (9, 12)
    insert into Table_1 (D_ID, Deposit_amt) values (10, 4)
    
    insert into Table_2 (Total_ID, Total_amt) values (1, 17)
    insert into Table_2 (Total_ID, Total_amt) values (2, 23)
    insert into Table_2 (Total_ID, Total_amt) values (3, 55)
    insert into Table_2 (Total_ID, Total_amt) values (4, 4)
    
    select t.Total_amt, 
        d1.D_ID as d1_ID, d1.Deposit_amt as d1_amt, 
        d2.D_ID as d2_ID, d2.Deposit_amt as d2_amt, 
        d3.D_ID as d3_ID, d3.Deposit_amt as d3_amt
    from Table_2 t
    cross join (
        select D_ID, Deposit_amt from Table_1 
    ) d1
    inner join (
        select D_ID, Deposit_amt from Table_1 
        union all
        select null, null
    ) d2 on d1.D_ID > d2.D_ID or d2.D_ID is null
    inner join (
        select D_ID, Deposit_amt from Table_1 
        union all
        select null, null
    ) d3 on d2.D_ID > d3.D_ID or d3.D_ID is null
    where isnull(d1.Deposit_amt, 0) + isnull(d2.Deposit_amt, 0) + isnull(d3.Deposit_amt, 0) = t.Total_amt
    order by Total_amt
    
    rollback tran
    

    Output:

    Total_amt   d1_ID       d1_amt      d2_ID       d2_amt      d3_ID       d3_amt
    ----------- ----------- ----------- ----------- ----------- ----------- -----------
    4           3           1           2           3           NULL        NULL
    4           4           1           2           3           NULL        NULL
    4           1           4           NULL        NULL        NULL        NULL
    4           10          4           NULL        NULL        NULL        NULL
    17          9           12          3           1           1           4
    17          9           12          4           1           1           4
    17          10          4           5           9           1           4
    17          8           7           7           6           1           4
    17          6           13          1           4           NULL        NULL
    17          10          4           6           13          NULL        NULL
    17          10          4           8           7           7           6
    17          8           7           5           9           4           1
    17          10          4           9           12          4           1
    17          8           7           5           9           3           1
    17          10          4           9           12          3           1
    17          6           13          3           1           2           3
    17          6           13          4           1           2           3
    23          8           7           6           13          2           3
    23          6           13          5           9           3           1
    23          6           13          5           9           4           1
    23          10          4           7           6           6           13
    23          10          4           9           12          8           7
    23          7           6           6           13          1           4
    23          9           12          8           7           1           4
    
    (24 row(s) affected)
    

    Note: You could filter out individual rows whose Deposit_amt > Total_amt, but this would probably not help performance much unless Deposit_amt was indexed.

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

Sidebar

Ask A Question

Stats

  • Questions 344k
  • Answers 344k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You can do this by retrieving the index of the… May 14, 2026 at 5:37 am
  • Editorial Team
    Editorial Team added an answer The ArraySegment<T> structure provides a view of an array without… May 14, 2026 at 5:37 am
  • Editorial Team
    Editorial Team added an answer I think the easiest way to do this is to… May 14, 2026 at 5:37 am

Related Questions

I have a table with these columns create table demo ( ID integer not
I am planning to implement a Type-2 SCD in PostgreSQL. The downside of SCDs
setup: mysql> create table bank(bank_id integer, bank_name varchar(255)); Query OK, 0 rows affected (0.27
I'd like to know a good solution for converting a LEFT JOIN into a
I'm trying to enforce integrity on a table in a way which I do

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.