Database Reference
In-Depth Information
11.1.2 Set Operations
SQL provides the ability to perform set operations, such as unions and
intersections, on rows of data. For example, suppose all the records in the orders
table are split into two tables. The orders_arch table, short for orders archived,
contains the orders entered prior to January 2013. The orders transacted in or after
January 2013 are stored in the orders_recent table. However, all the orders
for product_id 33611 are required for an analysis. One approach would be to
write and run two separate queries against the two tables. The results from the two
queries could then be merged later into a separate file or table. Alternatively, one
query could be written using the UNION ALL operator as follows:
SELECT customer_id,
order_id,
order_datetime,
product_id,
item_quantity AS qty
FROM orders_arch
WHERE product_id = 33611
UNION ALL
SELECT customer_id,
order_id,
order_datetime,
product_id,
item_quantity AS qty
FROM orders_recent
WHERE product_id = 33611
ORDER BY order_datetime
customer_id order_id order_datetime product_id qty
643126 13501-6446-6326-0182 2005-01-02 19:28:08 33611 1
725940 70738-4014-1618-2531 2005-01-08 06:16:31 33611 1
742448 03107-1712-8668-9967 2005-01-08 16:11:39 33611 1
.
.
.
640847 73619-0127-0657-7016 2013-01-05 14:53:27 33611 1
660446 55160-7129-2408-9181 2013-01-07 03:59:36 33611 1
647335 75014-7339-1214-6447 2013-01-27 13:02:10 33611 1
Search WWH ::




Custom Search