Please start any new threads on our new site at http://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

Our new SQL Server Forums are live! Come on over! We've restricted the ability to create new threads on these forums.

SQL Server Forums
Profile | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2012 Forums
 Transact-SQL (2012)
 Select second and third highest scores?
 Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Eagle_f90
Constraint Violating Yak Guru

USA
424 Posts

Posted - 09/06/2013 :  16:46:39  Show Profile  Visit Eagle_f90's Homepage  Reply with Quote
I have table Test_Scores with columns ID (int), StudentID (int), TestID (int), Score(int). How can I write a select statement that would return the second and third highest score?

--
If I get used to envying others...
Those things about my self I pride will slowly fade away.
-Stellvia

Lamprey
Flowing Fount of Yak Knowledge

4614 Posts

Posted - 09/06/2013 :  17:05:03  Show Profile  Reply with Quote
Do you know how to pick the top 3?
Go to Top of Page

cruellogic
Starting Member

1 Posts

Posted - 09/06/2013 :  17:20:45  Show Profile  Reply with Quote
I think this is what you are looking for.

;WITH CTE 
AS
(
SELECT ID, StudentID, TestID, Score, DENSE_RANK() OVER(ORDER BY Score DESC) AS R
FROM dbo.Test_Scores WITH(NOLOCK)
)
SELECT ID, StudentID, TestID, Score 
FROM CTE
WHERE R IN (2,3)
Go to Top of Page
  Previous Topic Topic Next Topic  
 Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.03 seconds. Powered By: Snitz Forums 2000