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

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Need help in query
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

mahdi87_gh
Yak Posting Veteran

Iran
72 Posts

Posted - 01/10/2013 :  02:23:28  Show Profile  Reply with Quote
Hi
I have a table named QuizHistory with these fields : quizId,userId,mark

I want to write a query to get a user's rank for a particular quizId

Thanks a lot

webfred
Flowing Fount of Yak Knowledge

Germany
8765 Posts

Posted - 01/10/2013 :  02:56:39  Show Profile  Visit webfred's Homepage  Reply with Quote
Do you have an example (example data and wanted result)?


Too old to Rock'n'Roll too young to die.
Go to Top of Page

mahdi87_gh
Yak Posting Veteran

Iran
72 Posts

Posted - 01/10/2013 :  03:12:58  Show Profile  Reply with Quote
assume here is the records in the table
quizId, userId, mark
1, 1, 85
1, 2, 72
1, 3, 94
1, 4, 65


the rank for userId=1 would be 2 (means he is the second person in this quiz)
the rank for userId=2 would be 3 (means he is the third person in this quiz)
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52317 Posts

Posted - 01/10/2013 :  03:22:24  Show Profile  Reply with Quote

SELECT userId,Rnk 
FROM
(
SELECT DENSE_RANK() OVER (PARTITION BY quizId ORDER BY mark) AS rnk,*
FROM table
)t
WHERE quizId=@Yourpassedquizid


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

mahdi87_gh
Yak Posting Veteran

Iran
72 Posts

Posted - 01/10/2013 :  04:29:48  Show Profile  Reply with Quote
thanks a lot.
it work's perfectly
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52317 Posts

Posted - 01/10/2013 :  04:35:26  Show Profile  Reply with Quote
welcome

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
  Previous Topic Topic Next Topic  
 New 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.05 seconds. Powered By: Snitz Forums 2000