TOP N BY GROUP

Hi.. I have table of sales reps and products and sales..  I need the top 10 sales persons by product


name             product              sales
john                tv                          100
mary              phone                    50
john               phone                    10
bob                radio                        5
mary             clock                        59




using sql 2005 !
LVL 1
JElsterAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mike EghtebasDatabase and Application DeveloperCommented:
;with cte
As
(
Select name, product, sales ,
dense_rank() Over (partition by product Order By sales Desc) As rk
From SalesRepTable Order By name;
)
Select name, product, sales  From cte
Where rk <= 10;

Modified...
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
JElsterAuthor Commented:
I think the server is 2000

'dense_rank' is not a recognized function name.


Any way to do in SQL2000?
0
Mike EghtebasDatabase and Application DeveloperCommented:
try:

Select Top 10 name, product, SumSale 
From 
( 
Select name, product, Sum(sales) As SumSale
From MyTable
Group By product, name
) As D ( name, product, SumSale)
Order By product, SumSale Desc, name 

Open in new window


modified...
0
JElsterAuthor Commented:
The ORDER BY clause is invalid in views, inline functions, derived tables, subqueries, and common table expressions

Any ideas?
0
Mike EghtebasDatabase and Application DeveloperCommented:
The ORDER BY clause is invalid in views, inline functions, derived tables, and common table expressions unless top(x) and offset fetch is utilized.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2005

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.