This is different from the one marked as a double, I want to sum up top 5 for each team. The double post takes out for each of the results in separate rows.
I'm using this question now but it seems that SQL is randomly returning 5 of for exasmple 10 rows and sum up, not the top 5. Anyone has some input for me?
select team, sum(length) as totalScore from (SELECT t.*, @num_in_group:=case when @team!=team then @num_in_group:=0 else @num_in_group:=@num_in_group+1 end as num_in_group, @team:=team as t FROM reg_catches t, (select @team:=-1, @num_in_group:=0) init ORDER BY team asc) sub WHERE sub.num_in_group<=4 and competition = 16 and team = 25 GROUP BY team ORDER BY totalScore DESC;
I'm struggeling on a SQL question that I can't get my head around. My result-table looks like below, I'm trying to sum up the top 5 result for each team and limit the output to the top 3 highest ranked teams. Everything was working as expected until I added my last score in the result-table. The output of my SQL now is randomly for team 25. I've expected that to be 520..
team length competition
----------------------
26 70 16
25 70 16
25 95 16
25 98 16
25 100 16
25 100 16
25 100 16
25 122 16
Output:
team totalScore
---- -----------
25 122
26 70
Wanted output:
team totalScore
---- -----------
25 522
26 70
SELECT team, SUM(length) AS totalScore
FROM(
SELECT team, length
FROM table_result m
WHERE competition = 16 and (
SELECT COUNT(*)
FROM table_result mT
WHERE mT.team = m.team AND mT.length >= m.length
) <= 5) tmp
GROUP BY team
ORDER BY totalScore DESC Limit 3
Anyone has any ideas for me?