SQL Query to find out which Shift produced most diesel engines

Question: Which shift (‘Shiftnumber’) produced most diesel vehicles (‘engine-type’)?

I have the table productionhistory

CREATE TABLE productionhistory (
ID int AUTO_INCREMENT PRIMARY KEY NOT NULL,
Shiftnumber int NOT NULL,
Carid int NOT NULL);

and the table vehicle

CREATE TABLE vehicle (
ID int AUTO_INCREMENT PRIMARY KEY NOT NULL,
`engine-type` varchar(6) NOT NULL);

vehicle.ID is the foreign key for productionhistory.carid.

Can somebody help me or give me a clue on how to solve this?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

You’ll need to join productionhistory with vehicle to obtain pairs of (Shiftnumber, enginetype). Now you can simply use WHERE engineType = ‘diesel’ GROUP BY … ORDER BY n DESC to obtain the shift in order of most produced engines of that type.

SELECT Shiftnumber
     , COUNT(*) AS n
  FROM productionhistory AS ph
  JOIN vehicle AS v
    ON ph.carid = v.id
   AND engineType = 'diesel'
 GROUP BY Shiftnumber
 ORDER BY n DESC
;


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x