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 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

Ask A Question

Stats

  • Questions 263k
  • Answers 263k
  • 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 I ended up using this: <dependency> <groupId>org.apache.openejb</groupId> <artifactId>javaee-api</artifactId> <version>5.0-2</version> <type>jar</type>… May 13, 2026 at 12:07 pm
  • Editorial Team
    Editorial Team added an answer While you cannot do this with the stock framework (no… May 13, 2026 at 12:07 pm
  • Editorial Team
    Editorial Team added an answer Sure - just pass an empty string: var arr2 =… May 13, 2026 at 12:07 pm

Related Questions

I have been putting off developing this part of my app for sometime purely
I have to maintain an old database which is not properly normalized. For instance
What is an effective algorithm for nesting 1 dimensional lengths into predefined stock lengths?
I have to make some changes in a small system that stores data in

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.