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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T12:19:10+00:00 2026-05-12T12:19:10+00:00

I have three tables, which are each 1:n. An entry in table1 has n

  • 0

I have three tables, which are each 1:n. An entry in table1 has n entries in table2, and so on. Let’s call them cars, wheels, and screws for illustration.
Screws can be clean(1) or rusty(2). I am joining them together, because I want to count two things. First, I want to have rows telling me how many good/bad screws per wheel I have for each car. So basically I am getting:

car_id    wheel_id   screw_state  count(screws)
   1          1           1          3
   1          1           2          7
   1          2           1          5
   1          2           2          3
   2          1           1          1
                ... and so on...

Now I want a second fact, namely how many rusty and clean screws I have for all wheels per car, without needing to know each specific number per wheel.
So basically now I just leave off the GROUP BY over wheel_id, like this:

car_id    screw_state  count(screws)
   1          1             8
   1          2            10
   2          1             1
                ... and so on...

The thing is, I would need both of them in one single query, because else I’d have a lot of sorting and rearranging to do.

I believe the second, easier count over the total screws per car should be done as a subquery, but can I join the first, bigger query easily with a subquery?

How is this done?

I would be happy over a quite specific answers, because I am not really an SQL wizard.

edit : I am working on an ORM, so funky thinks like below (hacking the col values to some constant) can’t be done there easily. I have to get this solution working there, so JOIN/subquery/UNIONs without funky workarounds would be great.

  • 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-12T12:19:11+00:00Added an answer on May 12, 2026 at 12:19 pm
    SELECT car_id, wheel_id, screw_state, count(screws)
      FROM cars C, wheels W, screws S
     WHERE W.car_id = C.car_id
       AND S.wheel_id = W.wheel_id
     GROUP BY car_id, wheel_id, screw_state
    UNION ALL
    SELECT car_id, -1 AS wheel_id, screw_state, count(screws)
      FROM cars C, wheels W, screws S
     WHERE W.car_id = C.car_id
       AND S.wheel_id = W.wheel_id
     GROUP BY car_id, screw_state
    ORDER BY car_id
    

    you can UNION 2 queries, the second one for all wheels per car, that’s why wheel_id = -1.

    result:

        car_id    wheel_id   screw_state  count(screws)
           1          1           1          3
           1          1           2          7
           1          2           1          5
           1          2           2          3
           1         -1           1          8
           1         -1           2         10
           2          1           1          1
           2         -1           1          1
    ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an existing database with tables which each have 4 partitions (there are
I have three tables which are defined as: class User(Base): __tablename__ = 'users' id
I have three tables of concern and a query which generates the following output
I have a table which needs to link one of three seperate tables, but
I have two tables which have some transactional stuff stored in them. There will
I have a SqlServer 2008 table which has a Primary Key (IsIdentity=Yes) and three
I have a table in HTML which has three columns i.e Error - User
I have a table PGM_MASTER, which has three columns PGM_ID | int(11) PGM_ENV_ID |
I have some hierarchical data - each entry has an id and a (nullable)
I have a table which gets around 10-15k entries per minute. Each one is

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.