SQL Server 查询的并交差集合运算的实际开发的实用性

在实际开发中,并交差集合运算的实用性如何?
2025-03-23 22:26:17
推荐回答(1个)
回答1:

将两条查询语句作为子查询的一部分连表.
select t1.count1+t2.count2 as 'countSum',t1.branch
(select count as count1, branch from table1 group by branch)t1
(select count as count2, branch from table2 group by branch)t2 on t1.branch =t2.branch
这种写法有一个潜在的限制,t1中的branch 必须包含t2,所以你的需求一般这么写:
select sum(counts)counts,branch
from(select * from(select count as counts, branch from table1 group by branch)t1
union
select * from(select count as counts, branch from table2 group by branch )t1)t
group by branch