ACC: Shortcut Method for Creating an SQL Union QueryLast reviewed: May 20, 1997Article ID: Q114731 |
The information in this article applies to:
SUMMARYNovice: Requires knowledge of the user interface on single-user computers. This article demonstrates a shortcut method for creating an SQL union query using a second "scratchpad" query to create the second half of the SQL UNION statement.
MORE INFORMATIONThe following example demonstrates how to create a union query that returns all the customers and suppliers in London in the sample database Northwind.mdb (or NWIND.MDB in version 2.0).
TipsNOTE: In the examples of SQL statements in the tips below, there are differences in the SQL syntax for Microsoft Access 2.0 and 7.0. In Microsoft Access 2.0 and 7.0, the word DISTINCTROW appears after each SELECT, for example:
SELECT DISTINCTROW Customers.CompanyNameIn Microsoft Access 2.0, because the field names contain spaces they are enclosed in square brackets ([]), for example:
SELECT DISTINCTROW Customers.[Company Name]In Microsoft Access 2.0, the field names used in a WHERE clause are not surrounded by an extra set of parentheses (), and they appear like this:
WHERE ((Suppliers.City="London"))To be able to tell which table the data comes from, switch back to the query's SQL view and add the table name (preceded by a comma and enclosed in quotation marks) to the end of each SELECT statement, as in this example:
SELECT Customers.CompanyName, Customers.ContactName, Customers.City, "Customers" FROM Customers WHERE (((Customers.City)="London")) UNION SELECT Suppliers.CompanyName, Suppliers.ContactName, Suppliers.City, "Suppliers" FROM Suppliers WHERE (((Suppliers.City)="London"));If you include the table name in the query, it appears in a fourth column called "Expr1003," when you run the query. To give the new column a more meaningful name, add an alias to it. For example, add the alias "[Source Table]" to the first SELECT statement as shown in this example:
SELECT Customers.CompanyName, Customers.ContactName, Customers.City, "Customers" AS [Source Table] FROM Customers WHERE (((Customers.City)="London")) UNION SELECT Suppliers.CompanyName, Suppliers.ContactName, Suppliers.City, "Suppliers" FROM Suppliers WHERE (((Suppliers.City)="London"));If you want to sort your query, add an ORDER BY statement to the last SELECT clause in the UNION statement as shown below:
SELECT Customers.CompanyName, Customers.ContactName, Customers.City, "Customers" AS [Source Table] FROM Customers WHERE (((Customers.City)="London")) UNION SELECT Suppliers.CompanyName, Suppliers.ContactName, Suppliers.City, "Suppliers" FROM Suppliers WHERE (((Suppliers.City)="London")) ORDER BY [ContactName]; REFERENCESFor more information about union queries, search for "union queries" and then "UNION Operation (Microsoft Jet SQL)" using the Microsoft Access 97 Help Index.
|
Additional query words: queries sql-specific short cut outer join
© 1998 Microsoft Corporation. All rights reserved. Terms of Use. |