更新表使用表B列的字段,其中表A的字段“_”分开,其对应的值在表B中

时间:2017-02-16 18:00:19

标签: oracle plsql

我有一个表ACTIVITY_KEY,其中包含以下字段:

COL1      COL2    COL3     COL4      COL5     COL6     COl7     
ACt1      A1_B1
ACT2      C1
ACT2      D1_D4
ACT3      A1_F1

字段COL3到COL7为空

现在还有另一个表KEYDEF,其中包含以下字段和值:

KEY       NAME        VAL
A1        COL4         UI
A1        COL6         2
B1        COL3         98
C1        COL5         N
C1        COL4         00
D1        COL3         1
D1        COL4         PP
D1        COL6         RT
D4        COL5          Y        
D4        COL7         DG 
F1        COL7         NN
F1        COL3         9T

我想用KEYDEF表中的值更新COL3-COL7的表ACTIVITY_KEY,它应该是这样的:

 COL1      COL2    COL3     COL4      COL5     COL6     COL7
 ACt1      A1_B1    98       UI                  2
 ACT2      C1                00         N
 ACT2      D1_D4     1       PP         Y       RT       DG
 ACT3      A1_F1    9T       UI                 2        NN   

我不知道怎么做,有人可以帮忙吗?

4 个答案:

答案 0 :(得分:1)

尝试此合并:

merge into ACTIVITY_KEY a
using (
    select
        a.col1,
        a.col2,
        max(k.col3) col3,
        max(k.col4) col4,
        max(k.col5) col5,
        max(k.col6) col6,
        max(k.col7) col7
    from ACTIVITY_KEY a
    left join (
        select *
        from keydef
        pivot (
            max(val) for name in (
                'COL3' as col3,
                'COL4' as col4,
                'COL5' as col5,
                'COL6' as col6,
                'COL7' as col7
            )
        )
    ) k on a.col2 like '%'||k.key||'%'
    group by a.col1, a.col2
) k on (
    a.col1 = k.col1
    and a.col2 = k.col2
)
when matched then update set
    a.col3 = k.col3,
    a.col4 = k.col4,
    a.col5 = k.col5,
    a.col6 = k.col6,
    a.col7 = k.col7;

正如@Matthew在评论中指出的,如果你可以发生碰撞。上述解决方案将采用该值的最大值。

答案 1 :(得分:0)

这是一种非常简单的方法。例如,假设您不会遇到ACTIVITY_KEY.COL2为" D4_F1"的情况,因为这会使COL7的值变得模棱两可。

select col1, col2,
 ( SELECT val FROM keydef d 
   WHERE ak.col2 LIKE '%' || d.key || '%' 
   AND d.name = 'COL3' 
   AND ROWNUM = 1) col3, 
 ( SELECT val FROM keydef d 
   WHERE ak.col2 LIKE '%' || d.key || '%' 
   AND d.name = 'COL4' 
   AND ROWNUM = 1) col4, 
 ( SELECT val FROM keydef d 
   WHERE ak.col2 LIKE '%' || d.key || '%' 
   AND d.name = 'COL5' 
   AND ROWNUM = 1) col5, 
 ( SELECT val FROM keydef d 
   WHERE ak.col2 LIKE '%' || d.key || '%' 
   AND d.name = 'COL6' 
   AND ROWNUM = 1) col6, 
 ( SELECT val FROM keydef d 
   WHERE ak.col2 LIKE '%' || d.key || '%' 
   AND d.name = 'COL7' 
   AND ROWNUM = 1) col7 
 from activity_key ak

SELECT(上面)感到满意后,您可以通过将列放在MERGE语句中来实际更新列。像:

MERGE TO ACTIVITY_KEY t
USING ( ... SELECT statement above ... ) u
ON ( t.col1 = u.col1 AND t.col2 = u.col2 )  -- assuming COL1+COL2 is unique...
WHEN MATCHED THEN UPDATE...

答案 2 :(得分:0)

只是一种不同的方法。希望它有所帮助。

MERGE INTO ACTIVITY_KEY C USING
(SELECT col1,
  COL2,
  MAX(COL3) COL3,
  MAX(COL4) COL4,
  MAX(COL5) COL5,
  MAX(COL6) COL6,
  MAX(col7) col7
FROM
  (SELECT B.COL1,
    B.COL2,
    CASE
      WHEN A.name = 'COL3'
      THEN A.VAL
    END COL3,
    CASE
      WHEN A.name = 'COL4'
      THEN A.VAL
    END COL4,
    CASE
      WHEN A.name = 'COL5'
      THEN A.VAL
    END COL5,
    CASE
      WHEN A.name = 'COL6'
      THEN A.VAL
    END COL6,
    CASE
      WHEN A.name = 'COL7'
      THEN A.VAL
    END COL7
  FROM KEYDEFF a,
    activity_key B
  WHERE (a.key=SUBSTR(B.COL2,1,2)
  OR a.key    =SUBSTR(B.COL2,LENGTH(b.col2)-1,LENGTH(b.col2)))
  )
GROUP BY COL1,
  COL2
)B ON (B.COL1 = C.COL1 AND B.COL2 = C.COL2)
WHEN matched THEN
  UPDATE
  SET COL3 = B.COL3,
    COL4   = B.COL4,
    COL5   = B.COL5,
    COL6   = B.COL6,
    col7   = b.col7;

答案 3 :(得分:0)

这是一个非常简单的查询来获得结果。

enter image description here ----------