I have a main table which is joined to a number of related tables.
The main table contains core details of lodges, and the other tabes contain information much of which can be one to many, e.g. news updates, or special offers.
This has the advantage of being able to tell is when each of these was last updated.
However, I know have a requirement to show a single date of when the last time any of a properties information was last updated.
i dunno, man… you’re cross-joining the many rows of news updates with the many rows of special offers
and there are other tables
a better approach is a UNION of MAXes, with a placeholder column for type of table updated, e.g. ‘news-update’ and ‘special-offer’
that gives you the latest updates for each types of tables per lodge
UNION means no need to worry about some lodge happening not to have any one type, e.g. special offers – which would mean your joins would have to be outer
SELECT * FROM TableA
UNION
SELECT * FROM TableB
EXCEPT
SELECT * FROM TableA
INTERSECT
SELECT * FROM TableB;
Change EXCEPT to MINUS for Oracle.
Slightly picky point: the above relies on operator precedence, which according to the SQL Standard is implementation dependent, so YMMV. It works for SQL Server, for which the precedence is:
Expressions in parentheses
INTERSECT
EXCEPT and UNION evaluated from left to right.