基于模式的SQL表

问题描述 投票:0回答:1

我有一个类似的表:

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列-例如,源中的janfeb中的值为target。反过来,源中的febaug中具有值target,依此类推augnov列中具有target因此new_target列将具有第3个值:即(跟踪在源和目标jan->feb->aug->nov之间,因为aug是第3个值,因此它是new_target列中的输出)

sql oracle oracle-sqldeveloper
1个回答
0
投票

这看起来像left join

select t.*, tnext.target
from t left join
     t tnext
     on t.target = t.next.source

0
投票

尝试一下:

select  m1.source, 
        m1.target, 
        m2.target as new_target
from mytable m1
left join mytable m2 on 
  m1.target = m2.source
© www.soinside.com 2019 - 2024. All rights reserved.