Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Beginner SQL Developer, need to exclude some result from query

Tags:

sql

I have an sql query to display all the sum of stage_times from the stage_time table. But I only require sum of stage_times for cars that have values entered for all 3 stages so car_no 3 is excluded from the list in this case;

Here is an sqlfiddle with my example code http://sqlfiddle.com/#!2/1675d/78

SELECT d.*, SUM(Stage_Time) AS total
FROM Driver d, Stage_Times st
WHERE st.Driver_Id = d.Driver_Id AND st.Event_Id = 1 
      AND st.Stage_Id <= 3 
GROUP BY d.Driver_Id
ORDER BY total;

Thanks in advance for any help

like image 719
Alan Mulligan Avatar asked Feb 02 '26 21:02

Alan Mulligan


1 Answers

You may just add an Having clause

having count(distinct st.Stage_id) = 3

see updated sqlfiddle

(by the way, I changed your query to use join, it's a better way to do).

like image 64
Raphaël Althaus Avatar answered Feb 04 '26 12:02

Raphaël Althaus



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!