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.
Lee Moore writes "Consider the following dataset. In trying to find the best score rank per person, I developed the following query, which returns duplicates...Human Date Score Test Rankjjones 5/1/1997 930 SAT 12lmiller 12/1/2003 900 SAT 12sdand 5/1/1999 930 SAT 12rdavis 12/1/1993 860 SAT 11tbaker 1/1/2002 730 SAT 8lmoore 5/1/1995 890 SAT 11asmith 6/1/1997 1030 SAT 15jjones 1/5/1995 13 ACT 6lmiller 1/1/2000 25 ACT 18sdand 1/1/2000 15 ACT 8rdavis 12/1/1990 15 ACT 8tbaker 4/1/2004 16 ACT 9lmoore 1/1/2000 9 ACT 2SELECT A.HUMAN, A.TEST, A.SCORE TEST_TEMP A WHERE A.RANK = (select max(b.score_rank) from test_temp b where b.id = a.id)Probably elementary, but I am stumped."
SamC
White Water Yakist
3467 Posts
Posted - 2005-10-11 : 09:08:09
Say hello to my leetle friend... (GROUP BY)
SELECT Human, Date, Score, Test, Rank FROM MyTable A INNER JOIN ( -- Subquery returns records set of Maximum Rank for each carbon-based life form SELECT Human, MAX(Rank) As MaxRank FROM MyTable GROUP BY Human ) B ON B.Human = A.Human AND B.MaxRank = A.Rank