View Single Post
Old 09-16-2019, 07:28 AM   #16
sxschech
Newly Registered User
 
Join Date: Mar 2010
Posts: 612
Thanks: 28
Thanked 112 Times in 101 Posts
sxschech is on a distinguished road
Re: How to filter a Union query?

Another approach is to wrap in () and then the query can behave as a standard select query. It will even show up in the graphical Design View. Of course, you won't be able to edit any contents within a particular SQL making up the union, but the overall Union query can then be treated and edited as a normal select query.

NOTE: Aliasing the overall set of tables as U for Union

Using the example query from these posts:

Code:
SELECT U.ID, U.T, U.N, U.C
FROM (
SELECT Table1.ID, Table1.T, Table1.N, Table1.C
FROM Table1
WHERE TABLE1.T<>'a'
ORDER BY Table1.T DESC
UNION
SELECT Table2.ID, Table2.T, Table2.N, Table2.C
FROM Table2
WHERE Table2.N=20
UNION 
SELECT Table3.ID, Table3.T, Table3.N, Table3.C
FROM Table3) AS U
WHERE U.C > 4
ORDER BY U.C DESC

sxschech is offline   Reply With Quote