Solved

SQL query to compile a list of game scores, eliminating progress scores, to sort by highest first

Posted on 2010-08-26
4
382 Views
Last Modified: 2013-11-05
I have a table that looks roughly like this (see attached). The "score" is a cumulative score. So I only want the last score entry for the game (games are identified by the "code" column, all records with the same "code" are part of the same game). The score I am looking for will have the most recent timestamp, and it should also be the highest score for the game (since negative scores are never produced).

So I basically want a list of "top 10 high scores". I'm not sure how to do this since all the non-last scores are in the table. I'm guessing some kind of grouping is necessary.

Thank you in advance for any assistance!
timestamp,code,score,question,answer
7/16/2010 2:40:28 PM,466163992316,0,673,False
7/16/2010 2:41:02 PM,466163992316,0,673,False
7/16/2010 2:41:11 PM,466163992316,0,673,False
7/16/2010 2:43:11 PM,466163992316,0,673,False
7/16/2010 4:56:05 PM,466163992316,78,1212,True
7/16/2010 4:56:05 PM,466163992316,78,1212,True
7/16/2010 5:00:02 PM,466163992316,0,2064,False
7/16/2010 5:00:03 PM,466163992316,0,2064,False
7/16/2010 10:21:50 PM,466163992316,0,2166,False
7/17/2010 11:59:12 AM,466163992316,74,1695,True
7/18/2010 10:27:33 PM,246810,60,9,True
7/18/2010 10:27:40 PM,246810,134,2,True
7/18/2010 10:27:44 PM,246810,222,3,True
7/18/2010 10:27:48 PM,246810,310,1,True
7/18/2010 10:27:51 PM,246810,400,5,True
7/18/2010 10:28:00 PM,246810,464,6,True

Open in new window

0
Comment
Question by:bbdesign
4 Comments
 
LVL 16

Expert Comment

by:vdr1620
ID: 33532907
Try using Rank Function as in below code

SELECT Top 10 * FROM
(
SELECT * , Rank () OVER (Partition By Code Order by Timestamp Desc) RankNo
)
WHERE RankNo = 1
ORDER BY ....
0
 
LVL 42

Accepted Solution

by:
dqmq earned 500 total points
ID: 33532929
select code, max(timestamp), max(score) group by code
order by max(score) desc
limit 10
0
 
LVL 1

Expert Comment

by:snoy
ID: 33532968
Hi,

what you mean by latest scores?
'coz if you wanna list the top 10 highest score there is no timestamp needed so a simple TSQL Select top 10.... will be enough

Can you give more explantions?

10x
H.Y.
www.w3consulting.ma
0
 

Author Comment

by:bbdesign
ID: 33533491
It looks like its that simple, thank you! I always get confused with grouping.
0

Featured Post

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
sql server concatenate fields 10 36
convert null in sql server 12 34
Whats wrong in this query - Select * from tableA,tableA 11 31
Show Results for Latest DateTime in a View 27 25
Nowadays, some of developer are too much worried about data. Who is using data, who is updating it etc. etc. Because, data is more costlier in term of money and information. So security of data is focusing concern in days. Lets' understand the Au…
Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question