0

Tried to come up with SQL query in MS Access, but null values and an aggregate function have me stumped. Any help appreciated.

Query to show records from TABLE1 where the EditDate (which may be null) is greater than the maximum LastImportDate from TABLE2.

TABLE1

Field Name - Data Type ReportID - Number EditDate - Date/Time

TABLE2

Field Name - Data Type LastImportDate - Date/Time

Thank you.

2
  • Do COALESCE or ISNULL work in Access's SQL? And if the EditDate is NULL, do you want to ignore it? You could probably use something like ... where ISNULL(TABLE1.EditDate, '1970-01-01') > TABLE2.LastImportDate... Commented Jun 23, 2014 at 16:26
  • Here's a link or working with ISNULL in MS Access. Something like this: ... where IIF(ISNULL(TABLE1.EditDate), '1970-01-01', TABLE1.EditDate) > TABLE2.LastImportDate... Commented Jun 23, 2014 at 16:31

1 Answer 1

0
SELECT *
FROM table1
WHERE editDate > (
    SELECT max(lastImportDate)
    FROM   table2
)

not sure how exactly that translates to an access query, but that is the idea.

Additionally, if you could break out the max date as a separate variable, that will get you a bit better performance - something like:

DECLARE @maxDate DATETIME
SET @maxDate = (
    SELECT max(lastImportDate)
    FROM   table2
)

SELECT *
FROM table1
WHERE editDate > @maxDate

Lastly, if you wanted to dates that have a null editDate, you can have the query interpret nulls as some arbitrary date like isNull(editDate, '1900-01-01') - this will make null editDates get interpreted as 1900 Jan 1

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.