Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Select record with nearest value

Status
Not open for further replies.

allanhicks

Programmer
Aug 19, 2001
4
0
0
JP
I have a table with fields FA & FD where FA is a key field and FD is a date. I want to know how to select, via sql, one record for each available key where the date is closest to but not more than a specified date.

eg if the table looked like:
FA, FD
1, 01/01/2000
1, 30/03/2001
1, 21/09/2001
2, 23/03/2001
2, 31/10/2001
3, 29/09/2001

and I asked for the records that were closest to 30/06/2001 I want to get back the following records:

FA, FD
1, 30/03/2001
2, 23/03/2001

Hope someone can help.
 
Try the following code:

SELECT FA, MAX(FB)
FROM your_table
WHERE FB <= max_date
GROUP BY FA

Remark: when no occurence of FB for a specific value x of FA is lower than max_date, no record is selected for the specific value x of FA...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top