2

使用字典映射更改字符串中的值有什么好技巧吗?例如我有table1 FIDDLE

+---------------------------+
|          ROW1             |
+---------------------------+
| This is an example string |
| This String has typ0s     |
+---------------------------+

还有一些映射表dict1 FIDDLE

+-------------------------+
|     OLD    |    NEW     |
+-------------------------+
| THIS       | THAT       |
| IS         | ARE        |
| EXAMPLE    | SOURCE     |
| STRING     | NUMBER     |
+------------+------------+

我需要一些SELECT语句来拆分值table1.row1并使用映射字典更改单词,dict1因此接收到的值将是(将不存在的字典值更改为上部是可选的):

+---------------------------+
|      TRANS_ROW1           |
+---------------------------+
| THAT ARE AN SOURCE NUMBER |
| THAT NUMBER HAS TYP0S     |
+---------------------------+

PS。使用 REGEXP 表达式进行拆分会非常好..

4

1 回答 1

1
WITH dict1 AS
 (SELECT 'THIS' fr,
         'THAT' t
    FROM dual
  UNION ALL
  SELECT 'IS' fr,
         'ARE' t
    FROM dual
  UNION ALL
  SELECT 'EXAMPLE' fr,
         'SOURCE' t
    FROM dual
  UNION ALL
  SELECT 'STRING' fr,
         'NUMBER' t
    FROM dual),
table1 AS
 (SELECT 'This is an example string' AS str,
         1 AS sn
    FROM dual
  UNION ALL
  SELECT 'This String has typ0s' AS str,
         2 sn
    FROM dual),
src AS
 (SELECT regexp_substr(upper(s.str), '[^ ]+', 1, LEVEL) str2,
         s.*,
         rownum nn
    FROM table1 s
  CONNECT BY instr(TRIM(' ' FROM str), ' ', 1, LEVEL - 1) > 0
         AND PRIOR sn = sn
         AND PRIOR dbms_random.value IS NOT NULL),
repl AS
 (SELECT nvl2(dict1.t, dict1.t, src.str2) lex,
         sn,
         nn
    FROM src
    LEFT JOIN dict1
      ON dict1.fr = src.str2)
SELECT listagg(lex, ' ') within GROUP(ORDER BY nn),
       sn
  FROM repl
 GROUP BY sn

它现在可以按您的要求工作。享受。

编辑:解决方案

于 2013-07-03T13:43:11.857 回答