|
||
|
Retrieving Data Action Queries Append Query versus Make Table Query Update Query Examples and Errors and VBA This Recordset is not updateable Statistical Analysis The most popular Microsoft Access Number Cruncher Source Code Library Royalty Free Source Code including ADO & DAO code Additional Resources MS Access Consulting and Programming Microsoft Access Developer Center Microsoft Access Products and Add-ins Connect with Us
|
UNION ALL ExampleThis SQL statement combines two queries to retrieve records based on states. The two queries happen to both get records from Tennessee ('TN'): SELECT City, State, Zip FROM Authors WHERE State IN ('KS', 'TN') Result of UNION ALL syntax:
Notice how this displays the two query results in the order they appear from the queries. The first two records come from the first SELECT statement, and the last two records from the second SELECT statement. The TN record appears twice, since both SELECT statements retrieve TN records. Union Query SQL ExampleUsing the same SQL statements and combining them with a UNION command: SELECT City, State, Zip FROM Authors WHERE State IN ('KS', 'TN') Result of UNION Query
Notice how the TN record only appears once, even though both SELECT statements retrieve TN records. The UNION syntax automatically eliminates the duplicate records between the two SQL statements and sorts the results. In this example the Corvallis record appears first but is from the second SELECT statement. A GROUP BY clause can be added at the end to sort the list. More Advanced Union QueriesThe example above is very simple for illustrative purposes, and can obviously be done without using a UNION query. More common uses of UNION queries include: More than two SELECT statementsYou can add SELECT statements with additional UNION syntax. You are not limited to just two. More complex SELECT statementsEach SELECT statement can include multiple tables with different types of joins and filters. Each query could be referencing completely different sets of tables. For instance, you can combine the list of zip codes with customers who purchased a particular product in the past year with zip codes that have total sales exceeding a certain amount. What's critical is the output field names are identical for each SELECT statement in the UNION query. The fields don't have to come from the same table, or even same field names since you can use the AS syntax to give the field a different name from its source. Creating an Artificial Blank RowYou can create a SELECT statement that simulates a blank row, without actually having a blank record in your table. This is commonly used as the rowsource of combo boxes to give people a choice of None (""). For example:
Here's an additional example in our Microsoft Access Query Tips and Techniques paper. Temporary Index Issues for UNION QueriesA UNION query, by definition, eliminates all duplicate rows (as opposed to UNION ALL) and is slower. To do this in SQL Server, it must build a temporary index on all the columns returned by both queries. If the index cannot be build for the queries, you will get a SQL error. In this situation, UNION ALL may be the solution if you don't have rights to do this. Alternatively, you may need to use a temporary table with INSERT queries to store the results of each query and use a standard SELECT query to group and get the unique values. Additional Resource for Union Queries
|
![]() |
Contact Us
l Web questions: Webmaster
l Copyright
© FMS, Inc., Vienna, Virginia |