Skip to content
Advertisement

Get row for each unique user based on highest column value

I have the following data

I am trying to find the distribution of each user’s Rating, based on their latest row in the table (latest is determined by Timestamp). On the path to that, I am trying to get a list of user IDs and Ratings which would look like the following

Trying to get here, I sorted the list on UserId and Timestamp (desc) which gives the following.

So now I just need to take the top N rows, where N is the number of players. But, I can’t do a LIMIT statement as that needs a constant expression, as I want to use count(id) as the input for LIMIT which doesn’t seem to work.

Any suggestions on how I can get the data I need?

Cheers! Andy

Advertisement

Answer

This should work:

If you can use WINDOW FUNCTIONS then you can use the following:

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement