Alison Balteramp;#039;s Mastering Microsoft Office Access 1002003 [Electronic resources] نسخه متنی

اینجــــا یک کتابخانه دیجیتالی است

با بیش از 100000 منبع الکترونیکی رایگان به زبان فارسی ، عربی و انگلیسی

Alison Balteramp;#039;s Mastering Microsoft Office Access 1002003 [Electronic resources] - نسخه متنی

Alison Balter

| نمايش فراداده ، افزودن یک نقد و بررسی
افزودن به کتابخانه شخصی
ارسال به دوستان
جستجو در متن کتاب
بیشتر
تنظیمات قلم

فونت

اندازه قلم

+ - پیش فرض

حالت نمایش

روز نیمروز شب
جستجو در لغت نامه
بیشتر
لیست موضوعات
توضیحات
افزودن یادداشت جدید



Union Queries


A Union query enables you to combine data from two tables with similar structures; data from each table is included in the output. For example, suppose you have a tblTimeCards table containing active time cards, and a tblTimeCardsArchive table containing archived time cards. The problem occurs when you want to build a report that combines data from both tables. To do this, you must build a Union query as the record source for the report. The syntax for a Union query is

Select-statement1 UNION [ALL]
Select-statement2 [UNION [ALL]
SelectStatement3] [...]

Here's an example:

SELECT FirstName, LastName, Department, Salary
FROM tblEmployees
UNION ALL SELECT FirstName, LastName, Department, Salary
FROM tblSummerEmployees

This example combines data from the tblEmployees table with data from the tblSummerEmployees table, preserving duplicate rows, if there are any.

The ALL Keyword


Notice the keyword ALL in the previous SQL statement. By default, Access eliminates all duplicate records from the query result. This means that if an employee is found in both the tblEmployees and tblSummerEmployees tables, he appears only once in the query result. Including the keyword ALL causes any duplicate rows to display.

Sorting the Query Results


When sorting the results of a Union query, the ORDER BY clause must be included at the end of the SQL statement. Here's an example:

SELECT FirstName, LastName, Department, Salary
FROM tblEmployees
UNION ALL SELECT FirstName, LastName, Department, Salary
FROM tblSummerEmployees
ORDER BY Salary

This example combines data from the tblEmployees table with data from the tblSummerEmployees table, preserving duplicate rows, if there are any. It orders the results by the Salary field (combining the data from both tables).

If the column names that you are sorting by differ in the tables included in the Union query, you must use the column name from the first table.

Using the Graphical QBE to Create a Union Query


You can use the graphical QBE to create a Union query. The process is as follows:


  • Click Queries in the list of objects in the Database window and double-click Create Query in Design view.

  • Click Close from the Show Tables dialog box without selecting a table.

  • Choose Query, SQL Specific, Union to open a SQL window.

  • Type in the SQL UNION clause. Notice that you can't switch back to the query's Design view (see Figure 11.33).

    Figure 11.33. An example of a Union query that combines tblTimeCards with tblTimeCardsArchive.

  • Click the Run button on the toolbar to execute the query.


  • CAUTION

    If you build a query and then designate the query as an SQL Specific query, you lose

    everything that you did prior to the switch. There is

    no warning, and Undo is not available!

    Important Notes about Union Queries


    It is important to note that the result of a Union query is not updateable. Furthermore, the fields in each SELECT statement are matched only by position. This means that you can get strange results by accidentally listing the FirstName field followed by the LastName field in the first SELECT statement, and the LastName field followed by the FirstName field in the second SELECT statement. Each SELECT statement included in a Union query must contain the same number of columns.


    / 544