Top 20 by group

jmersing

Well-known Member
Joined
Apr 14, 2004
Messages
887
I have a somewhat basic query, lets say its like this

select top 20 onefield, twofield, three field
from my table

This returns twenty records.

There are three types of values in onefield, I want to see the top twenty for each i.e. returning 60 rows.

Can't figure it out?
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
Can you show us some actual data and the actual layout of the results? That would make formulating the formulas/macros much simpler.
 
Upvote 0
I presume you could simply do something like this:

Code:
SELECT TOP 20 One, Two, Three FROM myTable WHERE One = 'firstvalue'
UNION
SELECT TOP 20 One, Two, Three FROM myTable WHERE One = 'secondvalue'
UNION
SELECT TOP 20 One, Two, Three FROM myTable WHERE One = 'thirdvalue'
 
Upvote 0
With a slight edit to Richard's SQL

Code:
SELECT TOP 20 One, Two, Three
FROM myTable
WHERE One = 'firstvalue'
ORDER BY One DESC
 
UNION
SELECT TOP 20 One, Two, Three
FROM myTable
WHERE One = 'secondvalue'
ORDER BY One DESC
 
UNION
SELECT TOP 20 One, Two, Three
FROM myTable
WHERE One = 'thirdvalue'
ORDER BY One DESC

Note 'firstvalue' assumes a text entry in field One. If it is numeric, change from
WHERE One = 'firsttext'
to
WHERE One = 123

HTH, Fazza
 
Upvote 0

Forum statistics

Threads
1,214,649
Messages
6,120,731
Members
448,987
Latest member
marion_davis

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top