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
gbnortonAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

oleggoldCommented:
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;
0
oleggoldCommented:
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

0
oleggoldCommented:
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;
0
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

gbnortonAuthor Commented:
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.
0
gbnortonAuthor Commented:
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.
0
Mark WillsTopic AdvisorCommented:
The problem is the INNER JOIN... You are only selecting when they are equal.

So, why not use the UNION to include those rows that do not match, but within a tolerance.

Assuming that the dates are close, but not identical and there will be only a matter of minutes difference in time. And there will only be one match within those minutes...

You could do :

SELECT qry_Glassing.DateTime as glassing_datetime, qry_Glassing.PN, qry_Glassing.Etch_Lot,  qry_furnace.datetime as Furnace_DateTime , qry_furnace.temp7_ave
FROM qry_Glassing, qry_Furnace
WHERE qry_glassing.DateTime = qry_Furnace.datetime
UNION
SELECT distinctrow qry_Glassing.DateTime, qry_Glassing.PN, qry_Glassing.Etch_Lot,  qry_furnace.datetime , qry_furnace.temp7_ave
FROM qry_Glassing, qry_Furnace
WHERE ((qry_glassing.DateTime <> qry_Furnace.datetime) and (abs(DateDiff("n",qry_glassing.DateTime ,qry_Furnace.datetime )) < 4 ) )

Open in new window


Now the first part is pretty much the INNER JOIN just expressed as an equi-join, the next (after the UNION) is picking up any other rows where datetimes are different but close...

There would be other strategies to collect top 1 within a grouping if there can be multiple rows within minutes of each other...
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
gbnortonAuthor Commented:
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
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Query Syntax

From novice to tech pro — start learning today.