📅  最后修改于: 2023-12-03 15:35:18.471000             🧑  作者: Mango
Teradata数据库提供了一些基本的集合运算符,如UNION,INTERSECT和EXCEPT。但是,有时候我们需要执行更高级的集合操作,例如合并多个结果集,或者使用聚合函数在集合上进行操作。这时候,我们可以使用Teradata SET运算符。
SELECT column1, column2, ...
FROM table1
<set operator>
SELECT column1, column2, ...
FROM table2;
-- SELECT语句定义多个子查询,并使用UNION ALL运算符将它们组合在一起
SELECT employee_id, hire_date
FROM employee
WHERE department = 'IT'
UNION ALL
SELECT employee_id, hire_date
FROM employee
WHERE department = 'Sales'
UNION ALL
SELECT employee_id, hire_date
FROM employee
WHERE department = 'Marketing';
-- 求两个表的共同行
SELECT emp_id
FROM employee
WHERE emp_salary > 50000
INTERSECT
SELECT emp_id
FROM employee
WHERE emp_bonus > 5000;
-- 查询两个表的差集
SELECT customer_id
FROM customer
MINUS
SELECT customer_id
FROM sales;