Link to home
Start Free TrialLog in
Avatar of gbnorton
gbnortonFlag for United States of America

asked on

access query find closest date and time to the minute

I'm using three queries to match records on Date and Time to the minute.  My current query works when there is an exact match

qry_Glassing returns a set of records that includes DateTime from table Glassing.  

qry_Furnace formats the DateTime to the minute on table Furnace.

qry_Temp7 joins qry_Glassing and qry_Furnace on DateTime.

Here is my current query:
SELECT qry_Glassing.DateTime AS GlassingTime, qry_Glassing.Etch_Lot, qry_Glassing.PN, qry_Furnace.DateTime AS FurnaceTime, qry_Furnace.Temp7_AVE
FROM qry_Glassing INNER JOIN qry_Furnace ON qry_Glassing.DateTime = qry_Furnace.DateTime
ORDER BY qry_Furnace.DateTime;

DateTime format
[DateTime]
6/6/2013 12:07 PM

When there is not an exact match for each record,  I need the next record that is closest to the minute:

Thanks,
Brooks
Avatar of oleggold
oleggold
Flag of United States of America image

try
SELECT qry_Glassing.DateTime AS GlassingTime, qry_Glassing.Etch_Lot, qry_Glassing.PN, qry_Furnace.DateTime AS FurnaceTime, qry_Furnace.Temp7_AVE
FROM qry_Glassing INNER JOIN qry_Furnace ON qry_Glassing.DateTime = qry_Furnace.DateTime
where qry_Furnace.DateTime=(select max(DateTime) from qry_Furnace where qry_Furnace.DateTime<= qry_Glassing.DateTime)
ORDER BY qry_Furnace.DateTime;
and formatted:
SELECT qry_Glassing.DateTime AS GlassingTime, qry_Glassing.Etch_Lot, qry_Glassing.PN, qry_Furnace.DateTime AS FurnaceTime, qry_Furnace.Temp7_AVE
FROM qry_Glassing INNER JOIN qry_Furnace ON qry_Glassing.DateTime = qry_Furnace.DateTime
where qry_Furnace.DateTime=(select max(DateTime) from qry_Furnace where qry_Furnace.DateTime<= qry_Glassing.DateTime)
ORDER BY qry_Furnace.DateTime;

Open in new window

also see if following would work:
SELECT qry_Glassing.DateTime AS GlassingTime, qry_Glassing.Etch_Lot, qry_Glassing.PN, qry_Furnace.DateTime AS FurnaceTime, qry_Furnace.Temp7_AVE
FROM qry_Glassing INNER JOIN qry_Furnace ON qry_Glassing.DateTime => qry_Furnace.DateTime
ORDER BY qry_Furnace.DateTime;
Avatar of gbnorton

ASKER

oleggold,
I tried your your suggestions with no joy.  Further observation of our data showed that the next record we need would be within 3 minutes.  So we wrote a routine to add 1, 2, or 3 minutes to the time.  Thanks for giving it a go.
I've requested that this question be closed as follows:

Accepted answer: 0 points for gbnorton's comment #a39562107

for the following reason:

Further observation of our data showed that the next record we need would be within 3 minutes.  So we wrote a routine to add 1, 2, or 3 minutes to the time.
ASKER CERTIFIED SOLUTION
Avatar of Mark Wills
Mark Wills
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
mark_wills,
Your suggestion gets me very close.  You last comment is accurate.  There are multiple rows within minutes of each other.  I'll post another question for that. I hope you'll take a look.
Thanks,
Brooks