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.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Group By clause

Author  Topic 

RoLYroLLs
Constraint Violating Yak Guru

255 Posts

Posted - 2010-02-10 : 17:02:39
WOW! I feel like I lost my SQL touch! I can't figure out a seemingly simple query.

Here's the data.

Score Table:

scoreid deviceid username score date
1 1 john 2098 1/1/2010 01:30:38
2 1 john 2098 1/1/2010 01:30:40
3 1 john 2098 1/1/2010 01:30:41
4 2 alan 2008 1/2/2010 16:45:25
5 2 alan 2008 1/2/2010 16:45:28
6 3 beth 6783 1/7/2010 13:10:56
7 1 alex 983 1/8/2010 08:00:06
8 1 john 3098 1/8/2010 10:50:42 <-- These johns are not the same
9 4 john 5098 1/8/2010 10:50:42 <--

The result I need ordered by score desc, date asc, but elimiting duplicate entries and only the top score for each deviceid and username (this is because the deviceid is not related the username, instead to the computer/device used and may have another username. ie: in the data above john and alex played on the same computer/device). Duplicate entries will be considered any entries with the same deviceid, the same username and the same score within 1 minute

scoreid deviceid username score date
6 3 beth 6783 1/7/2010 13:10:56
9 4 john 5098 1/8/2010 10:50:42
8 1 john 3098 1/8/2010 10:50:42
4 2 alan 2008 1/2/2010 16:45:25
7 1 alex 983 1/8/2010 08:00:06

The columns I need are just username, score and date. I placed the other columns there for visual explaination of the proper location for each row.

If someone can provide a query that actually removes any entries that have the same deviceid, same username, same score and within 1 minutes, that would be better.

- RoLY roLLs

http://www.buysellmotors.com
http://www.tikiloveroom.com
http://www.rolyrolls.com
http://iphone.rolyrolls.com

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-02-11 : 00:41:44
something like


SELECT columns.. FROM
(SELECT ROW_NUMBER() OVER (PARTITION BY deviceid,username ORDER BY score desc, date asc) AS Seq,other columns...
FROM Table
)t
WHERE Seq=1


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

Go to Top of Page

RoLYroLLs
Constraint Violating Yak Guru

255 Posts

Posted - 2010-02-11 : 01:52:25
WOW!!!!!!!!!!!!!

You are a genius!

Worked like a charm! (As far as I've tested all the possibilites that come to mind)

I was actually using the select row_number() on the outside and putting a subquery in that. Guess I was doing it backwards!

I'm going to look it up in the SQL Books, but in plain English terms can you tell me just a little about PARTITION BY, so i can compare my interpretation versus yours?

BTW: Game was fixed to not include duplicates now, but I needed a way to work with data I currently have. You saved me.

Thanks again!!!

- RoLY roLLs

http://www.buysellmotors.com
http://www.tikiloveroom.com
http://www.rolyrolls.com
http://iphone.rolyrolls.com
Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2010-02-11 : 02:14:00
In my simple words:
PARTITION BY controls when the row_number() has to start over again with value 1.


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2010-02-11 : 02:26:54
Also see how you can effectively use row_number() function
http://beyondrelational.com/blogs/madhivanan/archive/2007/08/27/multipurpose-row-number-function.aspx

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-02-11 : 02:31:15
quote:
Originally posted by RoLYroLLs

WOW!!!!!!!!!!!!!

You are a genius!

Worked like a charm! (As far as I've tested all the possibilites that come to mind)

I was actually using the select row_number() on the outside and putting a subquery in that. Guess I was doing it backwards!

I'm going to look it up in the SQL Books, but in plain English terms can you tell me just a little about PARTITION BY, so i can compare my interpretation versus yours?

BTW: Game was fixed to not include duplicates now, but I needed a way to work with data I currently have. You saved me.

Thanks again!!!

- RoLY roLLs

http://www.buysellmotors.com
http://www.tikiloveroom.com
http://www.rolyrolls.com
http://iphone.rolyrolls.com


welcome

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

Go to Top of Page
   

- Advertisement -