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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T13:28:49+00:00 2026-06-11T13:28:49+00:00

Here are the tables I have: AB tuple table C table which has entries

  • 0

Here are the tables I have:

AB tuple table
C table which has entries with A.id, B.id, C.units
D table which has entries with C.id
I want to count all the entries in D table which have a C.id that has the same A.id and B.id and subtract that count from the sum of all C.units that have the same A.id and B.id as a new column “difference”

So I want the query to return the “difference”, the common A.id and the common B.id in a single line

It should also return an entry if the count is 0 and the “difference” will just be be equal to sum(C.units)

For example

D table

D.id = 1, open=true, D.CID = 2
D.id = 2, open=true, D.CID = 3
D.id = 3, open=true, D.CID = 3
D.id = 4, open=true, D.CID = 4

C table

C.id = 2, A.id = 3, B.id = 5, units =4 
C.id = 3, A.id = 3, B.id = 5, units = 6
C.id = 4, A.id = 4, B.id = 6, units = 8
C.id = 5, A.id = 4, B.id = 6, units = 10

Bc the first 3 entries in D have CID’s with the same AID and BID they are counted in the same entry. Also, the C entries that have the same A.id and B.id have their units summed. Even when a C entry has no associated D entry. Therefore, the query should return the following 2 entries:

1. difference = (6+4)-3 = 7    A.id = 3  B.id = 5
2. difference = (10+8)-1 = 17  A.id = 4  B.id = 6
  • 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-11T13:28:50+00:00Added an answer on June 11, 2026 at 1:28 pm

    Setup (which you really should include in your question):

    CREATE TABLE c
      (
        id int NOT NULL PRIMARY KEY,
        aid int NOT NULL,
        bid int NOT NULL,
        units int NOT NULL
      );
    CREATE TABLE d
      (
        id int NOT NULL PRIMARY KEY,
        open boolean NOT NULL,
        cid int NOT NULL
      );
    INSERT INTO c VALUES (2,3,5,4),(3,3,5,6),(4,4,6,8),(5,4,6,10),(6,7,8,9);
    INSERT INTO d VALUES (1,true,2),(2,true,3),(3,true,3),(4,true,4);
    

    It’s a little hard to understand the question, but I think you might be looking for something like this:

    WITH n AS (
      SELECT aid, bid, count(*) AS cnt
        from c
        JOIN d ON (d.cid = c.id)
        GROUP BY aid, bid
    )
    SELECT aid, bid, sum(c.units) - COALESCE(n.cnt, 0) AS difference
      FROM c
      LEFT JOIN n USING (aid, bid)
      GROUP BY aid, bid, n.cnt
      ORDER BY aid, bid;
    

    I get these results:

     aid | bid | difference 
    -----+-----+------------
       3 |   5 |          7
       4 |   6 |         17
       7 |   8 |          9
    (3 rows)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here are the tables I have: Table A which has entries with item and
I have a question. I have here 2 tables table 1 : products product_id
Here's the scenario: I have 2 tables: CREATE TABLE dbo.API_User ( id int NOT
I have two tables: entitytype and project . Here are the create table statements:
So, here it goes: I have a table that is returning timespans for a
I have 2 tables here: Auction and Article. 1 Auction has 1 Article. 1
Hey all here are my tables i have placed in a DIV for each.
For ID =101 Data is coming from different TABLE All the TABLES have a
I used to have tables before to display the content and people here advised
Here's the scenario: I have 2 tables with data, one is the 2009 version

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.