Second Highest Number (1 Viewer)

mab9

Registered User.
Local time
Yesterday, 19:57
Joined
Oct 25, 2006
Messages
63
I'm trying to identify the 2nd highest number in a field in Access. While searching, I ran across the =large(a1:a3,2) formula, which works perfectly in Excel. I'm trying to duplicate this functionality in Access 2003, but it looks like this formula isn't in there. Any ideas on how to accomplish this?
 

pbaldy

Wino Moderator
Staff member
Local time
Yesterday, 17:57
Joined
Aug 30, 2003
Messages
36,140
One way would be 2 (or nested) queries; the first finds the top 2 values and the second gets the smaller of those 2. If I recall correctly, the MS Sample Queries db contains a sample of this.
 

CraigDolphin

GrumpyOldMan in Training
Local time
Yesterday, 17:57
Joined
Dec 21, 2005
Messages
1,582
Another way would be one query (finding the top two values as above) and using DMin on the field containing the values in the query.
 

Cosmos75

Registered User.
Local time
Yesterday, 19:57
Joined
Apr 22, 2002
Messages
1,281
Assuming we have a table called tblData with a number field (LONG) called lngValue, here are two ways to go about it.

Using DMax()

SELECT DISTINCT DMax("[lngValue]","tblData","[lngValue]<(SELECT Max(lngValue) FROM tbldata)") AS SecondHighest
FROM tblData AS a;


Using SELECT DISTINCT, TOP, and SELECT statement criteria

SELECT DISTINCT TOP 1 tblData.lngValue AS SecondHighest
FROM tblData
WHERE (tblData.lngValue<(SELECT DISTINCT TOP 1 Max(a.lngValue) from tblData as a))
ORDER BY tblData.lngValue DESC;



I'm sure there are other and better ways but here's what I came up with a few mins work.
 

Attachments

  • Example_Find2ndValue.zip
    8.2 KB · Views: 140

mab9

Registered User.
Local time
Yesterday, 19:57
Joined
Oct 25, 2006
Messages
63
I guess the be a little more specific, I'm building this into a form. On the detail section, this is going into the conditional formatting in order to highlight that entire row when the first or 2nd high numbers come up. For the max number, I'm using:

Expression is: [gam potential]=Max([GAM Potential])
 

Cosmos75

Registered User.
Local time
Yesterday, 19:57
Joined
Apr 22, 2002
Messages
1,281
mab9,

How about setting the conditional formatting to
Field Value equal to DMax("[lngValue]","tblData","[lngValue]<(SELECT Max(lngValue) FROM tbldata)")
 

Users who are viewing this thread

Top Bottom