View a unified result from multiple queries with a union query - Access - Office.pdf

download View a unified result from multiple queries with a union query - Access - Office.pdf

of 5

Transcript of View a unified result from multiple queries with a union query - Access - Office.pdf

  • 7/30/2019 View a unified result from multiple queries with a union query - Access - Office.pdf

    1/5

    When you want to review all of the data that is returned by several similar select qu eries together, as a combined

    set, use a union query.

    For example, if your database has two unrelated tables, one that stores customer data and another that stores

    data about suppliers and both tables have contact information fields, and youd like to see the contact

    information from both tables in one view. You would first create select queries for each table to retrieve only

    contact information, and then combine the results with a union query. Let us take a more detailed look at how to

    do this but firs, take a look at some requirements for a union query.

    NOTE The content in this article is intended for use with Access desktop databases. You cannot create or use a

    union query in Access apps.

    Step 1: Create the select queries

    NOTE Although you can include multiple tables or queries in a query, this procedure ass umes that each of

    your select queries includes data from only one table.

    On the Create tab, in the Queries group, click Query Design.1.

    In the Show Table dialog box, double-click the table that has the fields that you want to include. The table is

    added to the query design window.

    2.

    Close the Show Table dialog box.3.

    In the query design window, double-click each of the fields that you want to include.

    As you select f ields, make sure that you add the same number of fields, in the same order, that you add to theother select queries. Pay careful attention to the data types of the fields, and make sure that they have

    compatible data types with fields in the same position in the other queries that you are combining. For

    example, if your first select query has five fields, the first of which contains date/time data, make sure that

    each of the other select queries that you are combining also has five fields, the first of which contains

    date/time data, and so on.

    4.

    Optionally, add criteria to your fields by typing the appropriate expressions in the Criteria row of the field

    grid.

    5.

    After you have f inished adding fields and field criteria, you should run the select query and review its output.

    On the Design tab, in the Results group, click Run.

    6.

    Switch the query to Design view.7.

    Save the select query, and leave it open.8.

    Repeat this procedure for each of the select queries that you want to combine.9.

    For more information see applying criteria to a query.

    Step 2: Combine the select queries

    In this step, you would be creating the union query using SQL statements. (For more information, see the SQL

    syntax.)

    On the Create tab, in the Queries group, click Query Design.1.

    Close the Show Table dialog box.2.

    On the Design tab, in the Query group, click Union.3.

    The query design window is hidden, and the SQL view object tab is displayed. At this point, the SQL view

    object tab is empty.

    Click the tab for the first select query that you want to combine in the union query.4.

    On the Home tab, click View > SQL View.5.

    Copy the SQL statement for the select query. Click the tab for the union query that you started to create at

    step 1.

    6.

    Paste the SQL statement for the select query into the SQL view object tab of the union query.7.

    Delete the semicolon (;) at the end of the select query SQL statement.8.

    Press ENTER to move the cursor down one line, and then type UNION on the new line.9.

    Click the tab for the next select query that you want to combine in the union query.0.

    Repeat steps 5 through 10 until you have copied and pasted all of the SQL statements for the select queries1.

    View a unified result from multiple queries with a union query

    a unified result from multiple queries with a union query - Access - ... http://office.microsoft.com/en-us/access-help/view-a-unified-resu

    5 18-Apr-2013

  • 7/30/2019 View a unified result from multiple queries with a union query - Access - Office.pdf

    2/5

  • 7/30/2019 View a unified result from multiple queries with a union query - Access - Office.pdf

    3/5

    Type UNION, and then press ENTER.5.

    NOTE Do not use the ALL keyword when you use a union query to perform a full outer join.

    Press CTRL+V to paste the SQL code that you copied in step 3.6.

    In the code that you pasted, change LEFT JOIN to RIGHT JOIN.7.

    Delete the semicolon at the end of the second FROM clause, and then press ENTER.8.

    Add a WHERE clause that specifies that the value of the join field is NULL in the first table listed in the

    FROM clause (the left table).

    9.

    For example, if the FROM clause is:

    FROM Products RIGHT JOIN [Order Details]

    ON Products.ID = [Order Details].[Product ID]

    You would add the following WHERE clause:

    WHERE Products.ID IS NULL

    Type a semicolon (;) at the end of the WHERE clause to indicate the end of the union query.10.

    On the Design tab, in the Results group, click Run.

    The results of your union query appear in Datasheet view.

    11.

    For more information see,joining data sources in a query.

    Additional information

    Some requirements for a union query

    The select queries that you combine in a union query must have the same number of output fields, in the

    same order, and with the same or compatible data types. When you run a union query, data from each set of

    corresponding fields is combined into one output field, so that the query output has the same number of fields

    as each of the select statements.

    NOTE For the purposes of a union query, the Number and Text data types are compatible.

    A union query is SQL-specific, and therefore must be written directly in SQL. You wou ld switch to SQL View to

    write SQL-specific queries, including union queries.

    SQL syntax of a union query

    In a union query, each select query (also called a select statement) has the following clauses:

    CLAUSE CONTAINS A LIST OF

    SELECT fields that contain data that you want to retrieve

    FROM tables that contain those fields

    Optional WHERE Criteria for those fields.

    The select statements are combined by using the UNION keyword.

    The basic SQL syntax for a union query is as follows:

    SELECT field_1[, field_2,]

    FROM table_1[, table_2,]

    UNION [ALL]

    SELECT field_a[, field_b,...]

    FROM table_a[, table_b,];

    For example, if your database has a table named Products and another table named Services and both tables

    have fields that contain the name of the product or service, the price, warranty or guarantee availability, and

    whether you offer the product or service exclusively. Although the Products table stores warranty information,

    and the Services table stores guarantee information, the basic information is the same (whether a particular

    product or service comes with a promise of quality). You can use a union query, such as the following one, to

    combine the four fields from the two tables:

    a unified result from multiple queries with a union query - Access - ... http://office.microsoft.com/en-us/access-help/view-a-unified-resu

    5 18-Apr-2013

  • 7/30/2019 View a unified result from multiple queries with a union query - Access - Office.pdf

    4/5

    SELECT name, price, warranty_available, exclusive_offer

    FROM Products

    UNION ALL

    SELECT name, price, guarantee_available, exclusive_offer

    FROM Services;

    Let us examine the preceding syntax example line by line:

    SYNTAX EXPLANATION ADDITIONAL INFORMATION

    SELECT name, price,

    warranty_available,

    exclusive_offer

    First SELECT clause SELECT is followed by a list of identifiers that indicates thefields from which you want to retrieve data.

    A SELECT clause must list at least one field.

    This SELECT clause lists the field identifiers name, price,

    warranty_available, and exclusive_offer.

    FROM Products First FROM clause A FROM clause follows a SELECT clause, and together they

    form a basic select statement.

    FROM is followed by a list of identifiers that indicates which

    tables contain the fields that are listed in the SELECT clause.

    A FROM clause must list at least one table.

    This FROM clause lists the table identifier Products.

    UNION ALL The UNION

    keyword and the

    optional ALL

    keyword

    The results of the SELECT statement that precedes UNION

    will be combined with the results of the SELECT statement

    that follows UNION.

    When you use the ALL keyword, duplicate rows are not

    removed from the union query results.

    By using the ALL keyword, the query can run faster,

    because Access does not have to check for duplicate rows.

    Use the ALL keyword if any of the following conditions is

    true:

    You are certain that the select queries will not produce

    any duplicate rows.

    It does not matter if your results have duplicate rows.You want to see duplicate rows.

    SELECT name, price,

    guarantee_available,

    exclusive_offer

    Second SELECT

    clause

    Some Rules:

    The second SELECT clause must have the same number

    of fields as the first SELECT clause.

    Fields that share common data must appear in the same

    order in the clause.

    Fields that share common data must have the same or

    compatible data types.

    NOTE The names of the fields in the output of a union

    query are from the first SELECT clause. Hence, in theexample, data from the f ield "warranty_available" and from

    the field "guarantee_available" is named

    "warranty_available" in the query output.

    FROM Services Second FROM

    clause

    There are no restrictions on the tables in the FROM clauses

    of a union query. You can create a union query that uses

    the same tables in each FROM clause. You can have

    different numbers of tables in the FROM clauses. In our

    example, the FROM clauses each has only one table.

    Site Directory Contact Us Submit Feedback Legal Trademarks Privacy and Cookies Accessib ility 2013 Microsoft Corporation. All rights reserved.

    a unified result from multiple queries with a union query - Access - ... http://office.microsoft.com/en-us/access-help/view-a-unified-resu

    5 18-Apr-2013

  • 7/30/2019 View a unified result from multiple queries with a union query - Access - Office.pdf

    5/5

    a unified result from multiple queries with a union query - Access - ... http://office.microsoft.com/en-us/access-help/view-a-unified-resu