[英]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.