下面的列表是指婚前和婚后的人名。随着时间的推移,他们中的一些人离婚并再次结婚和/或改名。
我想要做的是获取该人一生中的所有姓名,并为每个人添加一个具有唯一标识符的新列。
这是名为 Names 的实际列表:
Name_before Name_after
Misti Gulick Misti Gulick Thibodeaux
Faye Leaton Faye Leaton Hemby
Arden Peck Arden Peck Mroz
Carlton Kingsley Carlton Kingsley Mcelveen
Dolly Verhey Dolly Verhey Irish
Gaynell Pasquale Gaynell Pasquale Ayala
Misti Gulick Thibodeaux Misti Thibodeaux
Faye Leaton Hemby Faye Hemby
Arden Peck Mroz Arden Mroz
Carlton Kingsley Mcelveen Carlton Mcelveen
Dolly Verhey Irish Dolly Irish
Gaynell Pasquale Ayala Gaynell Ayala
Misti Thibodeaux Misti Trey Thibodeaux
Faye Hemby Faye Barrett Hemby
Arden Mroz Arden Justin Mroz
Carlton Mcelveen Carlton Tameka Mcelveen
Dolly Irish Dolly Jeremiah Irish
Gaynell Ayala Gaynell Cherry Ayala
理想的清单应该是这样的:
Name_before Name_after Identifier
Misti Gulick Misti Gulick Thibodeaux Misti Gulick
Faye Leaton Faye Leaton Hemby Faye Leaton
Arden Peck Arden Peck Mroz Arden Peck
Carlton Kingsley Carlton Kingsley Mcelveen Carlton Kingsley
Dolly Verhey Dolly Verhey Irish Dolly Verhey
Gaynell Pasquale Gaynell Pasquale Ayala Gaynell Pasquale
Misti Gulick Thibodeaux Misti Thibodeaux Misti Gulick
Faye Leaton Hemby Faye Hemby Faye Leaton
Arden Peck Mroz Arden Mroz Arden Peck
Carlton Kingsley Mcelveen Carlton Mcelveen Carlton Kingsley
Dolly Verhey Irish Dolly Irish Dolly Verhey
Gaynell Pasquale Ayala Gaynell Ayala Gaynell Pasquale
Misti Thibodeaux Misti Trey Thibodeaux Misti Gulick
Faye Hemby Faye Barrett Hemby Faye Leaton
Arden Mroz Arden Justin Mroz Arden Peck
Carlton Mcelveen Carlton Tameka Mcelveen Carlton Kingsley
Dolly Irish Dolly Jeremiah Irish Dolly Verhey
Gaynell Ayala Gaynell Cherry Ayala Gaynell Pasquale
我试图做的是在 Name_before 中遇到来自 Name_after 的共同值,并重复执行,直到我没有更多匹配项。
每次创建这些表之一时,名称的数量都会减少。
create table name_temp1 as
select *
from Names
where Name_after in (select distinct(Name_before) from Names)
order by Name_before, Name_after;
create table name_temp2 as
select *
from name_temp1
where Name_after in (select distinct(Name_before) from name_temp1)
order by Name_before, Name_after;
create table name_temp3 as
select *
from name_temp2
where Name_after in (select distinct(Name_before) from name_temp2)
order by Name_before, Name_after;
然后我会使用带有“case”函数的查询:
select *,case when n3.Name_before=n2.Name_after
then case when n2.Name_before=n1.Name_after
then n1.Name_after else n.after end end end
from Names n, name_temp1 n1, name_temp2 n2, name_temp3 n3;
我知道这根本不优雅,也没有性能。你们中的一些人会帮助我改进它吗?或者甚至欢迎其他建议!谢谢,