Site Sponsored By: SQLDSC - SQL Server Desired State Configuration
Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
hello.i have a table that contains users scores log. Now i must select top 10 players who did the highest score.this is the query i use:select distinct(userid), max(score) as score, loggedfrom LFO_FlashGamesUsersScoresgroup by userid, loggedorder by score descthis is the result:userid score logged-----------------------------------------6420 39 2010-10-10 13:30:19.2636420 39 2010-10-10 13:35:51.8906420 39 2010-10-10 14:15:21.9836420 39 2010-10-10 14:12:21.0131176 39 2010-10-10 09:12:20.2001176 39 2010-10-10 09:16:37.7471176 39 2010-10-10 09:19:39.4976420 39 2010-10-09 13:08:50.3476420 39 2010-10-09 13:52:05.9076420 39 2010-10-09 13:58:17.643The problem is that it selects user multiple times. How do i make user appear only once with his highest score.thanks in advanced.
malpashaa
Constraint Violating Yak Guru
264 Posts
Posted - 2010-10-15 : 15:31:32
Try this:
SELECT TOP(10) userid, score, logged FROM (SELECT T.userid, T.score, T.logged, ROW_NUMBER() OVER(PARTITION BY T.userid ORDER BY T.score DESC) AS row_num FROM LFO_FlashGamesUsersScores AS T) AS T WHERE T.row_num = 1 ORDER BY score DESC;
eddyuk
Starting Member
4 Posts
Posted - 2010-10-16 : 09:48:24
That worked perfectly my friend :) thanks a lot, you saved that day!