给定三个表 Ta、Tb、Tc:
Ta(ID, Field1)
Tb(ID, Field2)
Tc(ID, Field3)
给定数据示例:
Ta
ID Field1
---------
1 A
1 B
Tb
ID Field2
---------
1 C
1 D
2 E
Tc
ID Field3
---------
1 F
2 G
2 H
问题: 我如何加入此数据以返回:
ID Field1 Field2 Field3
-----------------------
1 A C F
1 B D NULL
2 NULL E G
2 NULL NULL H
我以为我可以通过外连接实现这一点,但事实似乎并非如此。分组的顺序并不重要,只要我带回所有没有重复行的信息即可。
澄清一下。我真的不介意得到哪种组合,只要结果集以最少的行数返回所有数据即可。这是我正在尝试做的一个更现实的例子:
给定一个人,称他为约翰。他有两个电话号码和三个电子邮件地址:
PID Email
---------
John john@test.com
John john@mail.com
John john@john.com
PID Tel
--------
John 011
John 022
我要返回:
PID Email Tel
----------------------
John john@test.com 011
John john@mail.com 022
John john@john.com NULL
最佳答案
您可以通过以下方式接近:
select coalesce(ta.id, tb.id, tc.id), ta.field1, tb.field2, tc.field3
from (select ta.*, row_number() over (partition by id order by (select NULL)) as seqnum
from ta
) ta full outer join
(select tb.*, row_number() over (partition by id order by (select NULL)) as seqnum
from tb
) tb
on ta.id = tb.id and
ta.seqnum = tb.seqnum
(select tc.*, row_number() over (partition by id order by (select NULL)) as seqnum
from tc
) tc
on coalesce(ta.id, tb.id) = tc.id and
coalesce(ta.seqnum, tb.seqnum) = tc.seqnum
group by coalesce(ta.id, tb.id, tc.id),
coalesce(ta.seqnum, tb.seqnum, tc.seqnum)
order by 1, 2
不过正如我所说,在我的评论中,无法保证表中行的顺序,因此这些行可能不会按照您期望的顺序出现。使用示例数据,您可以使用:
over (partition by id order by field<n>)
如果字段定义顺序
关于sql server合并多个数据集不重复数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12691760/