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 2000 Forums
 Transact-SQL (2000)
 Top 5 per grouping in one select???

Author  Topic 

geckolumo
Starting Member

1 Post

Posted - 2005-09-20 : 02:02:34
all, (or anyone for that matter...)

I am a little stumped when it comes to attempting the following:
What I need is one select statment that selects the top 5 records for each group.
i.e. I would like the top 5 stores per region for the following query:

select Store_id, Store, Region, [% Chg Cap],[% Chg Rad]
from Performance_Variance
where [% chg RAD]< 0
and Month_end = '2005-09-14 23:59:59'
and Level = 'store'
Order by Region, Store_id

Any help on this is greatly appreciated!

tx
gex

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2005-09-20 : 02:12:35
Refer point 2 here
http://weblogs.sqlteam.com/mladenp/archive/2005/08/01/7421.aspx

Madhivanan

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

jen
Master Smack Fu Yak Hacker

4110 Posts

Posted - 2005-09-20 : 03:03:22
will this work?

select top 5 fields...
from table
group by region

--------------------
keeping it simple...
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2005-09-20 : 03:11:19
quote:
Originally posted by jen

will this work?

select top 5 fields...
from table
group by region

--------------------
keeping it simple...


No

It will select only top 5 records of the first group and not top 5 of each group

Madhivanan

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

- Advertisement -