I believe PostgreSQL (at least 8.3) will require that the DISTINCT ON expressions must match the initial ORDER BY expressions. I.E. you cannot use DISTINCT ON (accountid) if you have an ORDER BY score DESC . To fix this, add it to ORDER BY :
SELECT DISTINCT ON (accountid) * FROM scoretable ORDER BY accountid, score DESC LIMIT 10;
With this method, you can select all the columns in the table. It will only return 1 row per account, even if there are double "max" values ββfor the score.
This was useful for me, since I did not find the maximum score (which is easy to do with the max () function), but the account was last entered for the account.
orbitaudio
source share