Get top 2 records from sql

Crocdundee

Board Regular
Joined
May 10, 2010
Messages
174
Office Version
  1. 2013
Platform
  1. Windows
Hi, I wish to retrieve the top 2 records for every Track and Race No from a query on a table
Here is the code to query the table
====================================
SELECT Selections.Rcdate, Selections.Track, Selections.Rcno, Selections.Rctime, Selections.Tab, Selections.Horse, Selections.Runners, Selections.Pred
FROM Selections
WHERE (((Selections.Rcdate)=[Forms]![FrmSelections]![Combo0]) AND ((Selections.Runners)>7))
ORDER BY Selections.Track, Selections.Rcno, Selections.Pred DESC;
==============================================================================
Rcdate picks a date filter from a form
Runners asks for only records where more than 7 runners in the race
Pred is the field that we get the top 2 from it is a numerical field where data is from 100 downwards
Hope one can help here
Regards
Graham
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
Hi Crocdundee,


Check this query, i have tested it with dummy data, please add your criteria of rcddate and runners >7


SELECT s1.Track , s1.Pred
FROM Selections s1
WHERE (
SELECT COUNT(DISTINCT(s2.Pred))
FROM Selections s2
WHERE s2.Pred > s1.Pred and s1.Track = s2.Track )
in (0,1) group by s1.Track , s1.Pred
 
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