Any help to understand what is (not) happening would be appreciated. I have read most questions about sum NULL issues, googled and thought I understood how to deal with this; but in this bit of code I just can't see the solution.
The Brief: For my fellow players I would like to sum top 14 scores from the 22 rounds of our competition.
Working through the code for several days I have the result almost working, BUT just can't work a solution to the sum NULL issue. Player 5 should be scoring 15 not a NULL.
As soon as I increase the offset in the limit statement the query starts to attempt to add a NULL.
Help: Need to understand where I have failed and how to correct the query.
Thanks in advance.
idPlayerDetails
Rnd1 Rnd2 Rnd3 Rnd4 Rnd5 Rnd6 Rnd7 TopThree
1 0 19 18 15 18 24 0 61
2 0 21 18 10 17 13 0 56
3 0 15 0 17 17 23 0 57
4 0 21 21 21 18 19 0 63
5 0 0 0 0 0 15 0 NULL
6 0 24 20 20 18 20 0 64
8 0 25 18 14 0 25 0 68
9 0 0 0 0 0 19 0 NULL
SELECT idPlayerDetails
, IFNULL(MIN(CASE WHEN CompetitionRoundID = 1 THEN RoundScorecardPlayerPoints END),0) AS Rnd1
, IFNULL(MIN(CASE WHEN CompetitionRoundID = 2 THEN RoundScorecardPlayerPoints END),0) AS Rnd2
, IFNULL(MIN(CASE WHEN CompetitionRoundID = 3 THEN RoundScorecardPlayerPoints END),0) AS Rnd3
, IFNULL(MIN(CASE WHEN CompetitionRoundID = 4 THEN RoundScorecardPlayerPoints END),0) AS Rnd4
, IFNULL(MIN(CASE WHEN CompetitionRoundID = 5 THEN RoundScorecardPlayerPoints END),0) AS Rnd5
, IFNULL(MIN(CASE WHEN CompetitionRoundID = 6 THEN RoundScorecardPlayerPoints END),0) AS Rnd6
, IFNULL(MIN(CASE WHEN CompetitionRoundID = 7 THEN RoundScorecardPlayerPoints END),0) AS Rnd7
, SUM(CASE WHEN (RoundScorecardPlayerPoints, idRoundScorecard) >= (
SELECT RoundScorecardPlayerPoints, idRoundScorecard
FROM RoundScorecard AS x
WHERE x.idPlayerDetails = y.idPlayerDetails
ORDER BY RoundScorecardPlayerPoints DESC, idRoundScorecard DESC
LIMIT 2, 1
) THEN RoundScorecardPlayerPoints END) AS TopThree
FROM RoundScorecard y
GROUP BY idPlayerDetails