行列互换

涉及表:
rpt_1(
         id,
         relatioship1     ,
         related_cust_id1 ,
         relatioship2     ,
         related_cust_id2 ,
         relatioship3     ,
         related_cust_id3 ,
         relatioship4     ,
         related_cust_id4 ,
         relatioship5     ,
         related_cust_id5 ,
         relatioship6     ,
         related_cust_id6 ,
         relatioship7     ,
         related_cust_id7 ,
         relatioship8     ,
         related_cust_id8 ,
         relatioship9     ,
         related_cust_id9 ,
         relatioship10    ,
         related_cust_id10,
         relatioship11    ,
         related_cust_id11,
         relatioship12    ,
         related_cust_id12,
         relatioship13    ,
         related_cust_id13,
         relatioship14    ,
         related_cust_id14,
         relatioship15    ,
         related_cust_id15
)
obj_abc(
         id,
         code,
         name
)
需实现的结果:rpt_1表连(id)obj_abc表用code,name字段填充rpt_1的relatioship,related_cust_id字段,无值为null;id为rpt_1表主键,在表obj_abc中一个id下有N(N<=15)个code,name.
实现方法:
update rpt_1 rpt
    set (rpt.relatioship1     ,
         rpt.related_cust_id1 ,
         rpt.relatioship2     ,
         rpt.related_cust_id2 ,
         rpt.relatioship3     ,
         rpt.related_cust_id3 ,
         rpt.relatioship4     ,
         rpt.related_cust_id4 ,
         rpt.relatioship5     ,
         rpt.related_cust_id5 ,
         rpt.relatioship6     ,
         rpt.related_cust_id6 ,
         rpt.relatioship7     ,
         rpt.related_cust_id7 ,
         rpt.relatioship8     ,
         rpt.related_cust_id8 ,
         rpt.relatioship9     ,
         rpt.related_cust_id9 ,
         rpt.relatioship10    ,
         rpt.related_cust_id10,
         rpt.relatioship11    ,
         rpt.related_cust_id11,
         rpt.relatioship12    ,
         rpt.related_cust_id12,
         rpt.relatioship13    ,
         rpt.related_cust_id13,
         rpt.relatioship14    ,
         rpt.related_cust_id14,
         rpt.relatioship15    ,
         rpt.related_cust_id15) = 
        (select max(case when ron=1 then code  end),
                max(case when ron=1 then name  end),
                max(case when ron=2 then code  end),
                max(case when ron=2 then name  end),
                max(case when ron=3 then code  end),
                max(case when ron=3 then name  end),
                max(case when ron=4 then code  end),
                max(case when ron=4 then name  end),
                max(case when ron=5 then code  end),
                max(case when ron=5 then name  end),
                max(case when ron=6 then code  end),
                max(case when ron=6 then name  end),
                max(case when ron=7 then code  end),
                max(case when ron=7 then name  end),
                max(case when ron=8 then code  end),
                max(case when ron=8 then name  end),
                max(case when ron=9 then code  end),
                max(case when ron=9 then name  end),
                max(case when ron=10 then code  end),
                max(case when ron=10 then name  end),
                max(case when ron=11 then code  end),
                max(case when ron=11 then name  end),
                max(case when ron=12 then code  end),
                max(case when ron=12 then name  end),
                max(case when ron=13 then code  end),
                max(case when ron=13 then name  end),
                max(case when ron=14 then code  end),
                max(case when ron=14 then name  end),
                max(case when ron=15 then code  end),
                max(case when ron=15 then name  end)
         from (select id,code,name, row_number() over(partition by id order by code) ron from obj_abc) tmp
         where tmp.id = rpt.id
         );

你可能感兴趣的:(行列互换)