Author: Paul McNett
Posted: 2009-03-12 at 15:51:02
KAM.covad wrote:
> I have a table with a date field: dDate and thousands of records.
>
> The dates are scattered over time including the future. I need to find the record with the max date that is =< required date.
>
> How would you do this with sql select and also with Locate?
* sql select
* I use 2 statements because the first one will execute really fast without needing
* to pull in irrelevant records. Make sure you have an index on dDate.
* Note the final result could contain multiple records; you didn't specify how to
* determine the *one* record you need other than the specified date.
select max(dDate)
from <table>
where dDate <= ?
into array atemp
dDate = atemp[1]
select *
from <table>
where dDate == m.dDate
* locate
select <alias>
* index on dDate descending tag dDate_desc
set order to tag dDate_desc
set near on
locate for dDate = ?
* you are now on the first record with the date <= to the specified date.
Note my fox is rusty. I see I forgot the semicolons, for instance. So consider this
pseudocode.
Paul