basically on the following lines where I have the two cases, it keeps returning the ELSES even though track_id is matched in the driver stats table for that driver_id as that is what I am grouping the query by. If I am doing this wrong, then what I need is if there are no rows in the driver_stats table then it gets the value from the drivers table and the growth is set to 0. Any help is greatly appreciated.
Case s.value When track_id = (SELECT MAX(track_id) FROM driver_stats) Then s.value Else d.value End AS value,
Case s.growth When track_id = (SELECT MAX(track_id) FROM driver_stats) Then s.growth Else 0 End AS growth,
**//FULL QUERY**
SELECT d.drivers_id, d.drivername, t.code,
Case s.value When track_id = (SELECT MAX(track_id) FROM driver_stats) Then s.value Else d.value End AS value,
Case s.growth When track_id = (SELECT MAX(track_id) FROM driver_stats) Then s.growth Else 0 End AS growth,
SUM(IF(qual_pos = '1', 1,0)) AS poles,
SUM(IF(race_pos <= '3', 1,0)) AS podiums,
SUM(IF(race_pos = '1', 1,0)) AS victories,
COALESCE(SUM(overtakes),0) AS overtakes,
SUM(Case When s.track_id = (SELECT MAX(track_id) FROM driver_stats) Then points Else 0 End) lastracepoints,
COALESCE(SUM(points),0) AS points
FROM drivers d
Left Join driver_stats s
On s.drivers_id = d.drivers_id
Left Join teams t
On t.team_id = d.team_id
Group By d.drivers_id
ORDER BY d.drivers_id
Just for knowledge the table driver_stats has a row for each driver_id and for each track_id.
When you add the value between case and when, you should only add the value, and not the comparison.
Else MySQL will compare s.value to true, and it will be always false.
You have two options:
or
Anyway, in mySQL the next query would be easier to read: