I have a MySQL table where I store results from a racing championship, so that every rows contains -among other data- every driver\'s position in a certain race. I want to get t
Here's another way...
SELECT a.season
, a.driver
, SUM(points) T5
FROM
( SELECT x.season
, x.driver
, x.points
FROM results x
JOIN results y
ON (y.season = x.season
AND y.driver = x.driver)
AND (y.position < x.position OR (y.position = x.position AND y.race < x.race))
GROUP
BY x.season
, x.driver
, x.race
HAVING COUNT(*) <=5
) a
GROUP
BY season
, driver;