Filter top 5 values in filed

Crocdundee

Board Regular
Joined
May 10, 2010
Messages
174
Office Version
  1. 2013
Platform
  1. Windows
using Access 2007
Hi I would like to filter the max 5 records in a group recordset
Ideally in each groupby i would like to present the top 50% (but that maybe too much to ask for )???
But I would like to filter the top 5 records in the group which is [Trckid]

Code:
SELECT Neurals.Trckid, Neurals.Track, Neurals.Rcno, Neurals.Tb, Max(Neurals.NR) AS MaxOfNR
FROM Neurals
GROUP BY Neurals.Trckid, Neurals.RcDate, Neurals.Track, Neurals.Rcno, Neurals.Tb
HAVING (((Neurals.RcDate)=[Forms]![FRMSummary]![cboDateFrom]));
I can filter all records in the froup and I think i have to use "IN" but not sure where it fits into the SQL.....[NR] is the filter field
Any help here will be appreciated
Regards
Graham






TrckidRcDateTrackRcnoTbHorseNR
41504Devo0118/08/2013Devonport Synthetic15Hoop225
41504Devo0118/08/2013Devonport Synthetic11Crambazzled177
41504Devo0118/08/2013Devonport Synthetic12Dhanraj164
41504Devo0118/08/2013Devonport Synthetic13Hellova Street84
41504Devo0118/08/2013Devonport Synthetic14Solo Man76
41504Devo0218/08/2013Devonport Synthetic211Kenbiki199
41504Devo0218/08/2013Devonport Synthetic210Beryl Bee168
41504Devo0218/08/2013Devonport Synthetic23Costabitmore133
41504Devo0218/08/2013Devonport Synthetic25Funambulism122
41504Devo0218/08/2013Devonport Synthetic28Sharma Magic106
41504Devo0218/08/2013Devonport Synthetic22Come On Charger104

<tbody>
</tbody>
 
Last edited by a moderator:

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
Go to the queries properties and set the Top property to 5. F4 will bring up the properties pane.
 
Upvote 0
Hi Zack thanks for the help
I have looked and can not find the "Top Property " you describe
I opened the query in design sql mode , hit F4 but i can for the love of me not find the "top Property"
I did find on the ribbon the Return Value when I have options like 5, 25 100 or 5% etc etc but that wont work
It bring up only 5 records, as I want 5 records in every Group

Graham
 
Upvote 0
If you want to do this as a TOP n BY GROUP query, you will probably need to do it in 2 steps.
Here's an example that gets the top 5 invoice totals by client. There's a calculated field in the first query that adds the Invoice no * 0.000001 to generate a tie-break where one client has multiple invoices of the same value.

Start point: Invoice Totals with fields Company ID, Invoice No, Total (and other, irrelavent fields).
First query: qryInvoiceTotals Sorted
Code:
SELECT [Invoice Totals].[Company ID], [Invoice Totals].[Invoice No], [Invoice Totals].Total, [Total]+[invoice No]*0.000001 AS SortTotal
FROM [Invoice Totals]
WHERE ((([Invoice Totals].Total) Is Not Null))
ORDER BY [Invoice Totals].[Company ID], [Total]+[invoice No]*0.000001 DESC;

Second query: qryTop5InvoicesByClient
Code:
SELECT qryInvoiceTotalsSorted.[Company ID], qryInvoiceTotalsSorted.[Invoice No], qryInvoiceTotalsSorted.Total, (SELECT count(*) FROM qryInvoiceTotalsSorted AS q WHERE q.[Company ID] = qryInvoiceTotalsSorted.[Company ID] AND q.SortTotal >= qryInvoiceTotalsSorted.SortTotal) AS [Counter]
FROM qryInvoiceTotalsSorted
WHERE ((((SELECT count(*) FROM qryInvoiceTotalsSorted AS q WHERE q.[Company ID] = qryInvoiceTotalsSorted.[Company ID] AND q.SortTotal >= qryInvoiceTotalsSorted.SortTotal))<=5));

If you're not worried about ties you can do it in one step:
Code:
SELECT [Invoice Totals].[Company ID], [Invoice Totals].[Invoice No], [Invoice Totals].Total, (SELECT COUNT(*) FROM [Invoice Totals] AS q WHERE q.[Company ID]=[Invoice totals].[Company ID] AND q.Total>=[invoice Totals].total) AS [Counter]
FROM [Invoice Totals]
WHERE ((([Invoice Totals].Total) Is Not Null) AND (((SELECT COUNT(*) FROM [Invoice Totals] AS q WHERE q.[Company ID]=[Invoice totals].[Company ID] AND q.Total>=[invoice Totals].total))<=5))
ORDER BY [Invoice Totals].[Company ID], [Invoice Totals].Total DESC;

Denis
 
Upvote 0

Forum statistics

Threads
1,221,832
Messages
6,162,254
Members
451,757
Latest member
iours

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