[英]Merge difference of two tables in third table in SQL Server
您可以使用FULL OUTER JOIN
:
SELECT COALESCE(t1.[Date], t2.[Date]),
CASE
WHEN t1.[Group] IS NULL OR t2.[Group] IS NULL THEN NULL
ELSE t1.[Group]
END
FROM Table1 AS t1
FULL OUTER JOIN Table2 AS t2
ON t1.[date] = t2.[date] AND t1.[Group] = t2.[Group]
完全外部联接是答案,此外,我将添加第三列,以显示其中有数据的表。
select isnull(t1.[Date],t2.[Date]) [Date],
isnull(t1.[Group], t2.[Group]) [Group],
case
when t1.[Date] is null then 'Only on table 2'
when t2.[Date] is null then 'Only on table 1'
else 'On both tables'
end
from table1 as t1
full outer join Table2 as t2
on t1.[Date] = t2.[Date] AND t1.[Group] = t2.[Group]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.