Class Union
- Namespace
- YndigoBlue.Velocity.Model
- Assembly
- YndigoBlue.Velocity.dll
Represents a UNION operation that combines the result sets of two or more queries.
public class Union
- Inheritance
-
Union
Remarks
UNION combines rows from multiple Query objects into a single result set. All queries in a union must return the same number of columns with compatible data types.
There are two types of UNION operations:
- UNIONRemoves duplicate rows from the combined result set
- UNION ALLIncludes all rows, even duplicates (faster performance)
Unions are added to a base query using Union(Union). Multiple unions can be chained together to combine many queries.
Examples
Simple UNION of two queries:
Query activeQuery = new Query().Select(activeUsersTable["username"]).From(activeUsersTable);
Query archivedQuery = new Query().Select(archivedUsersTable["username"]).From(archivedUsersTable);
// Combine with UNION (removes duplicates)
activeQuery.Union(new Union(archivedQuery, unionAll: false));
using (ResultSet results = manager.Retrieve(activeQuery)) { ... }
// SQL: SELECT username FROM active_users
// UNION
// SELECT username FROM archived_users
UNION ALL to keep duplicates:
// Get all order totals from both current and historical orders
Query currentOrders = new Query().Select(ordersTable["total"]).From(ordersTable);
Query historicalOrders = new Query().Select(orderHistoryTable["total"]).From(orderHistoryTable);
// Combine with UNION ALL (keeps duplicates, faster)
currentOrders.Union(new Union(historicalOrders, unionAll: true));
using (ResultSet results = manager.Retrieve(currentOrders)) { ... }
// SQL: SELECT total FROM orders
// UNION ALL
// SELECT total FROM order_history
Multiple unions:
// Combine data from three different tables
Query emailsQuery = new Query().Select(emailsTable["email_address"]).From(emailsTable);
Query customersQuery = new Query().Select(customersTable["email"]).From(customersTable);
Query vendorsQuery = new Query().Select(vendorsTable["contact_email"]).From(vendorsTable);
// Chain multiple unions
emailsQuery.Union(new Union(customersQuery, false));
emailsQuery.Union(new Union(vendorsQuery, false));
// SQL: SELECT email_address FROM emails
// UNION
// SELECT email FROM customers
// UNION
// SELECT contact_email FROM vendors
UNION with WHERE clauses:
Query activeUsers = new Query()
.Select([ usersTable["username"], usersTable["email"] ])
.From(usersTable)
.Where(new Criterion<string>(usersTable["status"], "active"));
Query activeAdmins = new Query()
.Select([ adminsTable["username"], adminsTable["email"] ])
.From(adminsTable)
.Where(new Criterion<string>(adminsTable["status"], "active"));
activeUsers.Union(new Union(activeAdmins, false));
// SQL: SELECT username, email FROM users WHERE status = 'active'
// UNION
// SELECT username, email FROM admins WHERE status = 'active'
Constructors
- Union(Query, bool)
Creates a new UNION operation.
Properties
- Query
Gets the query to be combined with the base query.
- UnionAll
Gets whether this is a UNION ALL operation (true) or a UNION operation (false).