Académique Documents
Professionnel Documents
Culture Documents
http://office.microsoft.com/en-us/access-help/view-a-unified-result-from...
1. On the Create tab, in the Queries group, click Query Design. 2. 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. 3. Close the Show Table dialog box. 4. In the query design window, double-click each of the fields that you want to include. As you select fields, make sure that you add the same number of fields, in the same order, that you add to the other 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. 5. Optionally, add criteria to your fields by typing the appropriate expressions in the Criteria row of the field grid. 6. After you have finished 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. 7. Switch the query to Design view. 8. Save the select query, and leave it open. 9. Repeat this procedure for each of the select queries that you want to combine. For more information see applying criteria to a query.
1 dari 5
18-Apr-2013 12:23 PM
View a unified result from multiple queries with a union query - Access - ...
http://office.microsoft.com/en-us/access-help/view-a-unified-result-from...
into the SQL view window of the union query. Do not delete the semicolon or type anything following the SQL statement for the last select query. 12. On the Design tab, in the Results group, click Run. The results of your union query appear in Datasheet view.
SELECT field1, field2, ... "Product" AS type SELECT field1, field2, ... "Service" AS type
The query output would include a field named "type" that displays whether the row came from the Products table or the Services table. Each UNION keyword combines the SELECT statements that immediately precede and follow it. If you use the ALL keyword with some of the UNION keywords in your query, but not with others, the results will include duplicate rows from the pairs of SELECT statements that are combined by using UNION ALL, but will not include duplicate rows from the SELECT statements that are combined by using UNION without the ALL keyword. The number, data types, and order of the fields in the select queries must correspond. You can use expressions, such as calculations or subqueries, to make them correspond. For example, you can match a two-character year field with a four-character year field by using the Right function to extract the last two digits of the four-character year.
5. In the query design grid, on your union query's object tab, double-click the asterisk (*) to use all of the union query's fields to make a new table. 6. On the Design tab, in the Query Type group, click Make Table. 7. In the Make Table dialog box, type a name for the new table, optionally specify a different database in which to create the table, and then click OK. 8. On the Design tab, in the Results group, click Run.
2 dari 5
18-Apr-2013 12:23 PM
View a unified result from multiple queries with a union query - Access - ...
http://office.microsoft.com/en-us/access-help/view-a-unified-result-from...
Do not use the ALL keyword when you use a union query to perform a full outer join.
6. Press CTRL+V to paste the SQL code that you copied in step 3. 7. In the code that you pasted, change LEFT JOIN to RIGHT JOIN. 8. Delete the semicolon at the end of the second FROM clause, and then press ENTER. 9. 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). 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:
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 would switch to SQL View to write SQL-specific queries, including union queries.
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:
3 dari 5
18-Apr-2013 12:23 PM
View a unified result from multiple queries with a union query - Access - ...
http://office.microsoft.com/en-us/access-help/view-a-unified-result-from...
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 SELECT name, price, warranty_available, exclusive_offer EXPLANATION First SELECT clause ADDITIONAL INFORMATION SELECT is followed by a list of identifiers that indicates the fields 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 the example, data from the field "warranty_available" and from the field "guarantee_available" is named "warranty_available" in the query output.
FROM Services
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
Accessibility
4 dari 5
18-Apr-2013 12:23 PM
View a unified result from multiple queries with a union query - Access - ...
http://office.microsoft.com/en-us/access-help/view-a-unified-result-from...
5 dari 5
18-Apr-2013 12:23 PM