Re: Union query and exclusion of data




Get records that are in both A and B
SELECT A.X, "A" as Source
FROM A INNER JOIN B
ON A.X = B.X

Get Records in A that are not in B
SELECT A.X, "A" as Source
FROM A LEFT JOIN B
ON A.X = B.X
WHERE B.X is Null

Get Records in B that are not in A
SELECT B.X, "B" as Source
FROM A RIGHT JOIN B
ON A.X = B.X
WHERE A.X is Null

You should be able to use a union query to get
-- Records in A or in B but not in both (Last two queries above)
-- Records in A or B (all three queries)
-- Records in A (First two queries - although just a simple select of records in A will do this more efficiently)
-- Records in A and B or in B (First and third - again a simple select of records in B will do this more efficiently)


'====================================================
John Spencer
Access MVP 2002-2005, 2007-2008
Center for Health Program Development and Management
University of Maryland Baltimore County
'====================================================


Syed Zeeshan Haider wrote:
Hello Everybody,
I am usin Access 2003 Pro and trying to create a rather complex union query but don't seem to implement conditions.

Let's say I have two tables called A and B. A has column called x and B also has a column called x. I am trying to join these two columns into one column with following conditions:
If e is a member of both A and B then the query should include e from table A only (because the second column calculates a value which depends on the choice of table for picking e). I have tried all INNER JOIN, LEFT JOIN and RIGHT JOIN but none of them have worked so far.

Here is an example of what I did:

select [A].[x]
FROM [A] INNER JOIN [B] ON [A].[x] = [B].[x]
where (([A].[x]) Is Not Null) and (([A].[x])<>([B].[x]))
UNION select [B].[x]
from [B];

Any ideas what am I doing wrong here?
Any help will be highly appreciated.

Thank you,
.



Relevant Pages

  • Re: Create query from list in a table
    ... add a comboboxfor the selection of the Team. ... Steve Clark, Access MVP ... There are 15 sub queries that individully produce the information that I ... I also have one query that uses INNER JOIN to put all that data into ...
    (microsoft.public.access.queries)
  • Re: Reporting from Queries
    ... check the At Your Survey demo at ... >MS Access MVP ... I tried the union query but it only works ... >> different tables or queries. ...
    (microsoft.public.access.reports)
  • Re: Union Query
    ... Do you have an example of a select query you changed to a Union query (i.e., ... change back to Access 2000 when you can either rewrite the queries as select ... your A2K database backup. ... <Access MVP> ...
    (microsoft.public.access.queries)
  • Re: system resource exceeded
    ... > The temp file is created as required, but if you fill 2Gig, that surely ... > If you make a table from the first four queries and use that table, ... >> One inner join to the same type field to each of the five attached ... >>> Vanderghast, Access MVP ...
    (microsoft.public.access.queries)
  • Re: system resource exceeded
    ... The temp file is created as required, but if you fill 2Gig, that surely ... If you make a table from the first four queries and use that table, ... > One inner join to the same type field to each of the five attached ... >> Vanderghast, Access MVP ...
    (microsoft.public.access.queries)