Filter top 1 in field

Crocdundee

Board Regular
Joined
May 10, 2010
Messages
174
Office Version
  1. 2013
Platform
  1. Windows
Hi I made this query qith the following sql

SELECT RS.[Rcdate], RS.[Trk], RS.[Rn], RS.[Tab], RS.[Horse], RS.[APrice], RS.[CP]
FROM RS
WHERE (((RS.[Rcdate])=#11/20/2015#) AND ((RS.[APrice])<25))
ORDER BY RS.[Trk], RS.[Rn], RS.[CP] DESC;

I wish to filter the top 1 in [cp]
grouped by [rsdate], [trck], [rn]

any help here would be appreciative
Graham
and [aprice] stays same at <25
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
Try this:
Code:
SELECT RS.[Rcdate], RS.[Trk], RS.[Rn], RS.[Tab], RS.[Horse], RS.[APrice], RS.[CP]
FROM RS
 INNER JOIN 
    (SELECT RS1.Rcdate, RS1.Trk, RS1.Rn, Max(RS1.CP) AS MaxOfCP 
    FROM RS AS RS1 WHERE (RS1.Rcdate=#11/20/2015#) And (RS1.[APrice]<25) 
    GROUP BY RS1.Rcdate, RS1.Trk, RS1.Rn)  AS Q1 
ON (RS.Rcdate=Q1.Rcdate) AND (RS.Trk=Q1.Trk) AND (RS.rn=Q1.rn) AND (RS.cp=Q1.MaxOfCP);
 
Upvote 0
Thank you Joe4 for your help it works perfectly and I would never been able to do it.
I applaud all you Programmers whom dedicate your time and knowledge to both Excel and access
Thanks
Graham
 
Upvote 0
Your welcome!

As opposed to using the Subquery method I used above, you could do the same thing in a series of two queries.
The first one would just be an Aggregate Query which groups by the fields you mentioned and takes the Max CP value.
Then link that Aggregate Query back to your original table, linking on all the fields in the Aggregate Query, and returning all the fields from the original query that you want.

Though it is two queries, the advantage to this other approach is that it could be done entirely with the Query Builder and not require you to explicitly write your own SQL code.
 
Upvote 0
Yes That I will try , another question please how can I change the SQL to list top 3 in [cp]

Regards
Graham
 
Upvote 0
Use the method described here: How to select top 10 in Access query? - Stack Overflow
You may have to incorporate it with the methods I described above.

If you run into some issues, please post a data sample and expected output (I am not 100% clear if your want the top 3 before or after your grouping, but an example will show us clearly what you are after).
 
Upvote 0

Forum statistics

Threads
1,221,831
Messages
6,162,252
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