Return maximum date from 4 tables (in 1 query)

Darren Bartrup

Well-known Member
Joined
Mar 13, 2006
Messages
1,297
Office Version
  1. 365
Platform
  1. Windows
I have four tables, each with a dteRefresh field which contains a date value.
Is there a way of returning the maximum date within the four tables in a single query.
I could do it by running a union query and then creating another query to return the maximum date, but I'm hoping there's a way of doing it in a single query - something like the following:

Code:
SELECT MAX(

SELECT dteRefresh
FROM tbl_CORE_AssessmentValidation

UNION SELECT dteRefresh
FROM tbl_CORE_CarersValidation

UNION SELECT dteRefresh
FROM tbl_CORE_JointValidation

UNION SELECT dteRefresh
FROM tbl_CORE_ReviewValidation

)

Thanks for any help.
Darren.
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
Hi Darren

You can use a subquery. Your syntax was very close. Try:

Code:
SELECT MAX(dteRefresh) AS Max_dteRefresh
FROM (
    SELECT dteRefresh
    FROM tbl_CORE_AssessmentValidation
 
    UNION SELECT dteRefresh
    FROM tbl_CORE_CarersValidation
 
    UNION SELECT dteRefresh
    FROM tbl_CORE_JointValidation
 
    UNION SELECT dteRefresh
    FROM tbl_CORE_ReviewValidation
);
 
Upvote 0
It's so obvious when shown!
I really need to keep up to speed with my SQL.

Thanks for that PGC.
 
Upvote 0
Any idea why, after I've saved the query and go back in to look at it, it's showing:

Code:
SELECT MAX(dteRefresh) AS Max_dteRefresh
FROM [SELECT dteRefresh
    FROM tbl_CORE_AssessmentValidation
 
    UNION SELECT dteRefresh
    FROM tbl_CORE_CarersValidation
 
    UNION SELECT dteRefresh
    FROM tbl_CORE_JointValidation
 
    UNION SELECT dteRefresh
    FROM tbl_CORE_ReviewValidation
]. AS [%$##@_Alias];

The query still works, but I've never seen it do that before.
 
Upvote 0
Hi again

The subquery only exists inside this query. Since you did not name it, access names it with a default Alias. You need it to refer to the subquery, for ex. in an ORDER BY clause. In that case, however, you should replace the default Alias (that is meaningless) by your own, to improve readability.
 
Upvote 0
Again, pretty obvious when you think about it.

I was having one other problem, which is that the query worked before I saved it - but afterward I'd get an error message.
I solved this by taking out the spacing in the query.
Then I put the spaces back in so I could post what it was doing (removing part of the table name to show: tbl_CORE_Vali) but now it works fine with the line spacing to.

Of course I blame this on Bill Gates. The same as I blame him for brussel sprouts.
 
Upvote 0

Forum statistics

Threads
1,214,983
Messages
6,122,588
Members
449,089
Latest member
Motoracer88

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top