How you do it depends on whether you want to return columns from the
referencing (child) table other than the completion date. If you only want
columns from the refernced (parent) table and the date from the referencing
table then a simple aggregating query is all that's needed, e.g.
SELECT [SomeField], [SomeOtherField],
MAX ([Completion Date] AS [LatestDate]
FROM [FirstTable] INNER JOIN [SecondTable]
ON [FirstTable].[Report Number] = [SecondTable].[Report Number]
WHERE [Completion Date] IS NOT NULL
GROUP BY [SomeField], [SomeOtherField];
If you want columns other than CompletionDate from the referenced table
(lets assume a column [PhaseNumber] from the second table) then you need to
use a subquery to identify the latest date:
SELECT [SomeField], [SomeOtherField]
[PhaseNumber], [Completion Date] AS [LatestDate]
FROM [FirstTable] INNER JOIN [SecondTable] AS ST1
ON [FirstTable].[Report Number] =ST1.[Report Number]
WHERE ST1.[Completion Date] =
(SELECT MAX([Completion Date])
FROM [SecondTable] AS ST2
WHERE ST2.[Report Number] = ST1.[Report Number]);
Note how in the second query the different instances of the second table are
distinguished by aliases ST1 and ST2, allowing the subquery to be correlated
with the outer query. Its not necessary to test for IS NOT NULL in the
subquery, as if all the completion date columns for a particular report
number are NULL the subquery will return NULL. ST2.[Completion Date] = NULL
will evaluate to NULL, never to TRUE as comparing anything with NULL, even
NULL, always results in NULL. This is because NULL is an absence of a value,
an unknown, so comparing anything with an unknown must result in another
unknown.
Ken Sheridan
Stafford, England
dsc2bjn said:
I need to be able to find the latest date of multiple record which belong to
a parent record, but only if all the child records have an actual completion
date.
I have all my children records in a table linked to the parent record using
a [Report Number].
Any suggestions would be appreciated.
dsc2bjn said:
I need to be able to find the latest date of multiple record which belong to
a parent record, but only if all the child records have an actual completion
date.
I have all my children records in a table linked to the parent record using
a [Report Number].
Any suggestions would be appreciated.