Results 1 to 2 of 2
  1. #1
    dougie is offline Novice
    Windows XP Access 2007
    Join Date
    Dec 2011
    Posts
    27

    How to make a union query from tables with different structures

    I'm offering this is as a tip as I've recently found it useful in reducing the number of queries in an application - apologies if it's old hat. I have three tables with nothing in common but a date field. I want to collect a number and date from each table and then do things with those numbers (group by month, divide, multiply, total, cross tab etc.). Suppose table1 has fields Date, Quantity, table2 has fields Date, Price and table3 has fields Date, NumberOfWheels. I can make a union as follows:

    SELECT Date, Quantity, 0 AS Price, 0 AS NumberOfWheels
    FROM table1
    UNION ALL
    SELECT Date, 0 AS Quantity, Price, 0 AS NumberOfWheels
    FROM table2
    UNION ALL
    SELECT Date, 0 AS Quantity, 0 AS Price, NumberOfWheels
    FROM table3;

    If you have a WHERE clause it must be appended to all three FROM statements, like this:

    SELECT Date, Quantity, 0 AS Price, 0 AS NumberOfWheels
    FROM table1 WHERE Year(Date)=2012
    UNION ALL
    SELECT Date, 0 AS Quantity, Price, 0 AS NumberOfWheels
    FROM table2 WHERE Year(Date)=2012
    UNION ALL
    SELECT Date, 0 AS Quantity, 0 AS Price, NumberOfWheels
    FROM table3 WHERE Year(Date)=2012;

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    Yes, this is known but congratulations for discovering on your own. Note that it is not necessary to repeat the alias field names in each UNION line. This should also work:

    SELECT Date, Quantity, 0 AS Price, 0 AS NumberOfWheels FROM table1 WHERE Year(Date)=2012
    UNION ALL SELECT Date, 0, Price, 0 FROM table2 WHERE Year(Date)=2012
    UNION ALL SELECT Date, 0, 0, NumberOfWheels FROM table3 WHERE Year(Date)=2012;

    Could use Null as placeholder instead of 0 or text.

    However, I am not sure why a query that joins the 3 tables would not serve.

    Also, Date is a reserved word. Should avoid reserved words as field names, otherwise enclose in [].
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Union Query Help
    By pmp in forum Queries
    Replies: 4
    Last Post: 10-28-2011, 06:41 AM
  2. Union Query
    By jlclark4 in forum Queries
    Replies: 3
    Last Post: 02-25-2011, 08:21 PM
  3. Replies: 2
    Last Post: 09-01-2010, 09:51 PM
  4. Replies: 4
    Last Post: 04-09-2010, 02:16 AM
  5. UNION two tables with same primary key values
    By carillonator in forum Queries
    Replies: 1
    Last Post: 02-02-2010, 08:54 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums