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
 General SQL Server Forums
 New to SQL Server Programming
 TOP 3 PER GROUP

Author  Topic 

nobby
Yak Posting Veteran

58 Posts

Posted - 2007-06-11 : 16:17:16
I Know how to group and i know how to return the Top n
But how do i return the top n results per group.

dinakar
Master Smack Fu Yak Hacker

2507 Posts

Posted - 2007-06-11 : 16:21:23
search in these forums and you will find threads which asked excat same question.

Dinakar Nethi
************************
Life is short. Enjoy it.
************************
http://weblogs.sqlteam.com/dinakar/
Go to Top of Page

nobby
Yak Posting Veteran

58 Posts

Posted - 2007-06-11 : 18:01:12
Hi
I have i found this example and have got it working but purely luck i think.
can someone explain what each part is doing. because i don't understand exactly why this works.

ELECT CustomerID FROM (
SELECT CustomerID, OrderNumber, ROW_NUMBER() OVER (PARTITION BY CustomerID ORDER BY OrderDate DESC) AS RecID
) AS d
WHERE RecID BETWEEN 1 AND 3


Thanxs
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2007-06-12 : 08:52:04
It generate Serail No for each customer Id and select three records
Also refer point 2
http://weblogs.sqlteam.com/mladenp/archive/2005/08/01/7421.aspx


Madhivanan

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

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2007-06-12 : 09:46:13
a little more on it here:

http://weblogs.sqlteam.com/jeffs/archive/2007/03/30/More-SQL-Server-2005-Solutions.aspx

- Jeff
http://weblogs.sqlteam.com/JeffS
Go to Top of Page
   

- Advertisement -