I have 2 tables:
Table 1 (tbl_Temp) contains temperature data for a particular park at a certain date/time.
Table 2 (tbl_Surveys) contains survey data that was conducted at a certain date/time
I need to find the nearest date/time for each survey in Table 2 to Table1 and import the temperature data into Table 2. While this sounds like a simple problem I've only been able to do it using a series of queries - pretty much because of the need to run an aggregate query to find the minimum date difference. I also want to do this in the background (via VBA) and I'm not entirely sure how to link 2 SQL statements in VBA. Does anyone have a more elegant solution than my series of queries (see below)? Thanks!
Query1:
SELECT tbl_Surveys.Park, Format([SurveyDate],"General Date") AS Survey_Date, tbl_Temp.[Date(ET)], Abs(([SurveyDate]-[Date(ET)])*24) AS DateDiff, tbl_Temp.[Temperature(F)], Year([SurveyDate]) AS [Year]
FROM tbl_Surveys
INNER JOIN tbl_Temp ON tbl_Surveys.Park = tbl_Temp.PARK;
Query2:
SELECT Query1.Park, Query1.Survey_Date, Min(Query1.DateDiff) AS MinOfDateDiff
FROM Query1
GROUP BY Query1.Park, Query1.Survey_Date
ORDER BY Query1.Park, Query1.Survey_Date, Min(Query1.DateDiff);
Query3:
SELECT Query2.Park, Query2.Survey_Date, Query1.[Date(ET)], Query2.MinOfDateDiff, Query1.[Temperature(F)]
FROM Query1 INNER JOIN Query2 ON (Query1.DateDiff = Query2.MinOfDateDiff) AND (Query1.Survey_Date = Query2.Survey_Date) AND (Query1.Park = Query2.Park)
ORDER BY Query2.Park, Query2.Survey_Date;
Table 1 (tbl_Temp) contains temperature data for a particular park at a certain date/time.
Table 2 (tbl_Surveys) contains survey data that was conducted at a certain date/time
I need to find the nearest date/time for each survey in Table 2 to Table1 and import the temperature data into Table 2. While this sounds like a simple problem I've only been able to do it using a series of queries - pretty much because of the need to run an aggregate query to find the minimum date difference. I also want to do this in the background (via VBA) and I'm not entirely sure how to link 2 SQL statements in VBA. Does anyone have a more elegant solution than my series of queries (see below)? Thanks!
Query1:
SELECT tbl_Surveys.Park, Format([SurveyDate],"General Date") AS Survey_Date, tbl_Temp.[Date(ET)], Abs(([SurveyDate]-[Date(ET)])*24) AS DateDiff, tbl_Temp.[Temperature(F)], Year([SurveyDate]) AS [Year]
FROM tbl_Surveys
INNER JOIN tbl_Temp ON tbl_Surveys.Park = tbl_Temp.PARK;
Query2:
SELECT Query1.Park, Query1.Survey_Date, Min(Query1.DateDiff) AS MinOfDateDiff
FROM Query1
GROUP BY Query1.Park, Query1.Survey_Date
ORDER BY Query1.Park, Query1.Survey_Date, Min(Query1.DateDiff);
Query3:
SELECT Query2.Park, Query2.Survey_Date, Query1.[Date(ET)], Query2.MinOfDateDiff, Query1.[Temperature(F)]
FROM Query1 INNER JOIN Query2 ON (Query1.DateDiff = Query2.MinOfDateDiff) AND (Query1.Survey_Date = Query2.Survey_Date) AND (Query1.Park = Query2.Park)
ORDER BY Query2.Park, Query2.Survey_Date;