SQL合并查询

SQL Query for Consolidation

我需要一点帮助...

我有两个数据表:

Table 编程电路(理论行程) Table 个卖家涵盖的线路(实际行程) 要达到的结果:

谢谢...

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

我想你想要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 服务器支持 using,那么会有一个简单的替代方案。)