我有一个类似的表:
source target
jan feb
mar apr
jun
feb aug
apr jul
oct dec
aug nov
dec may
输出(我要在其中创建new_target列的位置:
source target new_target
jan feb aug
mar apr jul
jun
feb aug nov
apr jul
oct dec may
aug nov
dec may
目标是基于类似-的逻辑创建new_target
列-例如,源中的jan
在feb
中的值为target
。反过来,源中的feb
在aug
中具有值target
,依此类推aug
在nov
列中具有target
因此new_target
列将具有第3个值:即(跟踪在源和目标jan->feb->aug->nov
之间,因为aug
是第3个值,因此它是new_target
列中的输出)
这看起来像left join
:
select t.*, tnext.target
from t left join
t tnext
on t.target = t.next.source
尝试一下:
select m1.source,
m1.target,
m2.target as new_target
from mytable m1
left join mytable m2 on
m1.target = m2.source