Use Rank function in SQL Server 2005

Today another problem beats me again. I have a table Score

ID int PK

Name varchar

Score int

Date datetime

and my member asks me to retrieve all records of 10 last days with theirs max score. Again, another help from a friend of mine (the guy I mentioned in previous article) and bro Google.

The query is:

WITH PageScore (ID, Date, Score, PosNo)

AS

(

SELECT ID, Date, Score,

Dense_rank() OVER (ORDER BY Score desc, date desc)

AS PosNo

FROM Score

where date in

(select distinct top 10 date

from Score

order by date desc)

)

 

SELECT * FROM PageScore

WHERE PosNo <= 10;

 

 

Another great day!

Nguyen Minh Dung

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: