
November 21st, 2012, 11:06 AM
|
|
|
id is not unique, so it cannot be PRIMARY, but based upon what you have said, we'll assume that the there is instead a compound PK formed on (id,rank)...
Code:
SELECT id
, SUM(ppl) ttl
FROM
( SELECT x.*
, COUNT(*)rnk
FROM my_table x
JOIN my_table y
ON y.id = x.id
AND y.rank <= x.rank
GROUP
BY id,rank
) a
WHERE rnk <=3
GROUP
BY id;
...or something like that
|