用于合并的SQL查询

时间:2018-04-17 15:13:36

标签: sql sql-server consolidation

我需要一些帮助...

我有两个数据表:

编程电路表(理论行程) enter image description here 卖家所涵盖的电路表(真实行程) enter image description here 要实现的结果:

enter image description here 感谢...

2 个答案:

答案 0 :(得分:0)

select isnull(a.date, b.date), isnull(a.seller, b.seller), a.[theoretical itinerary], b.[real itinerary] 
from table1 a full join table2 b 
on a.date=b.date and a.seller=b.seller

答案 1 :(得分:0)

我想你想要full join

select coalesce(t.date, r.date) as date, coalesce(t.seller, r.seller) as seller,
       t.itinerary as theoretical_itinerary,
       r.itinerary as real_itinerary
from theoretical t full join
     real r
     on t.date = r.date and t.seller = r.seller;

coalesce()是使用full join的不幸神器。如果您不包含它,那么您将看到匹配列的NULL值。 (如果只支持SQL Server using,则会有一个简单的替代方案。)