左连接和指示器匹配
Left join and indicator matching
所以我是 sql 的新手,需要一些编码帮助。
这是我的桌子
Table_A
Customer ID_2019
A 600
A 697
A 932
Table_B
Customer ID_2018
A 600
A 697
这是我合并两个表时的代码
--合并结果
select a.*,b.id_2018 into #temp
from Table_A as a left join Table_B as b
on a.customer=b.customer;
这是我的结果:
Results
Customer ID_2019 ID_2018
918 600 600
918 600 697
918 697 600
918 697 697
918 932 600
918 932 697
Here are the results that I desire:
--Desired Results
Customer ID_2019 ID_2018
918 600 600
918 697 697
918 932 -
我想将客户的 ID_2019 和 ID_2018 合并在一起;对于第三个 ID_2019 (932),我希望 ID_2018 有空格或破折号。
--我想在那里放一个指示符,看看 ID_2019 是否匹配 ID_2018 并创建一个带有 1 或 0 的新变量。
--drop table #match_nomatch;
select *,
case when ID_2019=ID_2018 then 1 else 0 end as Match into #Match_NoMatch
from #temp
group by customer, ID_2019, ID_2018;
这是我的结果:
--The results for ID_2018 are repeating the same 2 id's over again
Customer ID_2019 ID_2018 Match
918 600 600 1
918 600 697 0
918 697 600 0
918 697 697 1
918 932 600 0
918 932 697 0
这是我想要的结果:
Desired Results
Customer ID_2019 ID_2018 Match
918 600 600 1
918 697 697 1
918 932 - 0
对于上面的代码,如果 ID_2019 和 ID_2018 匹配,则输入 1,否则输入 0。同样对于第三个 Id_2019,我想要 ID_2018为空白或破折号。
如有任何帮助,我们将不胜感激。
您需要一个用于联接的列来分配行号。你没有,所以你可以发明一个:
select a.*, b.id_2018
into #temp
from (select a.*, row_number() over (order by id) as seqnum
from Table_A a
) a left join
(select b.*, row_number() over (order by id) as seqnum
from Table_B b
) b
on a.customer = b.customer and a.seqnum = b.seqnum;
您可以使用 full outer join
和 customer, id_2018 and id_2019
如下:
select coalesce(a.customer,b.customer) as customer,
a.id_2019, b.id_2018
into #temp
from Table_A as a full join Table_B as b
on a.customer=b.customer and a.id_2019 = b.id_2018;
所以我是 sql 的新手,需要一些编码帮助。 这是我的桌子
Table_A
Customer ID_2019
A 600
A 697
A 932
Table_B
Customer ID_2018
A 600
A 697
这是我合并两个表时的代码 --合并结果
select a.*,b.id_2018 into #temp
from Table_A as a left join Table_B as b
on a.customer=b.customer;
这是我的结果:
Results
Customer ID_2019 ID_2018
918 600 600
918 600 697
918 697 600
918 697 697
918 932 600
918 932 697
Here are the results that I desire:
--Desired Results
Customer ID_2019 ID_2018
918 600 600
918 697 697
918 932 -
我想将客户的 ID_2019 和 ID_2018 合并在一起;对于第三个 ID_2019 (932),我希望 ID_2018 有空格或破折号。
--我想在那里放一个指示符,看看 ID_2019 是否匹配 ID_2018 并创建一个带有 1 或 0 的新变量。
--drop table #match_nomatch;
select *,
case when ID_2019=ID_2018 then 1 else 0 end as Match into #Match_NoMatch
from #temp
group by customer, ID_2019, ID_2018;
这是我的结果:
--The results for ID_2018 are repeating the same 2 id's over again
Customer ID_2019 ID_2018 Match
918 600 600 1
918 600 697 0
918 697 600 0
918 697 697 1
918 932 600 0
918 932 697 0
这是我想要的结果:
Desired Results
Customer ID_2019 ID_2018 Match
918 600 600 1
918 697 697 1
918 932 - 0
对于上面的代码,如果 ID_2019 和 ID_2018 匹配,则输入 1,否则输入 0。同样对于第三个 Id_2019,我想要 ID_2018为空白或破折号。
如有任何帮助,我们将不胜感激。
您需要一个用于联接的列来分配行号。你没有,所以你可以发明一个:
select a.*, b.id_2018
into #temp
from (select a.*, row_number() over (order by id) as seqnum
from Table_A a
) a left join
(select b.*, row_number() over (order by id) as seqnum
from Table_B b
) b
on a.customer = b.customer and a.seqnum = b.seqnum;
您可以使用 full outer join
和 customer, id_2018 and id_2019
如下:
select coalesce(a.customer,b.customer) as customer,
a.id_2019, b.id_2018
into #temp
from Table_A as a full join Table_B as b
on a.customer=b.customer and a.id_2019 = b.id_2018;