【问题标题】:ORACLE: parse embedded Id and match lookupORACLE:解析嵌入的 Id 并匹配查找
【发布时间】:2016-08-18 04:56:56
【问题描述】:

我正在寻找一个有趣的谜语的帮助。刚刚发现可变长度的 id 嵌入在不符合要求的文本字段中。此 id 对系统链接至关重要。有一个 4-12 位数字 ID 的维护列表,该列表相当准确,因此可以与描述性注释字段 VarChar2(30) 中嵌入的 ID 相匹配

我的目标平台是 Oracle 12c 和 DataStage 8.5 来执行此操作。期望的结果是简单地列出具有适当 4-12 位数字 id 的列,并为任何不匹配列出空值。

扫描 NOTE_DESC 字符以查找字符串中的序列号。在数字字符串的每个术语上,对照查找列表检查结果值。就像是 loop1(表中的每一行)... loop2(每个 NOTE_DESC 字符)...如果是数字,则输入 loop3(每个 listvalue)以使用嵌套 if 语句检查列表中的每个数字以删除长度(regexp_substr(substr(trim(t .NOTE_DESC),,6),'[0-9]+'))=

这种方法会占用大量资源,因为查找列表中有数百个。我很好奇是否有人遇到过这样的问题或有解决此问题的代码。

数据示例: 独立维护的不同 IDS 的查找列表:{123, 1234, 5678, 12345, 123456, 1234567 } 同时这里是 NOTE_DESC 字段示例的列表:

  • JS 1234 已关闭(与所需结果 1234 匹配)
  • 123456 5月裁决(与123456期望结果匹配)
  • 关闭 Bal 5678(与 5678 的期望结果匹配)
  • 12-3 平衡调整(不匹配因此无效)
  • 1-23-45 Johnson(不匹配,因此结果为空)
  • 供应商 123489(不匹配,因此结果为空)

【问题讨论】:

  • 如果有多个匹配项,选择哪个有关系吗?也许它们出现在 NOTE_DESC 字段中的顺序是第一个?或者 ID 中是否有优先顺序?或者如果 NOTE_DESC 值与多个 ID 匹配,结果是否应该不止一行?此外,您知道 2345 将匹配 23 和 234(以及 45),对吗?或者只有当完整的连续数字字符串匹配时才匹配(因此甚至不应该查看数字的“子字符串”)?在我的示例中,只有 2345 会匹配,但 234 不会匹配 ABC2345-200?

标签: oracle


【解决方案1】:

这可能会(也可能不会)有帮助。首先,我通过 WITH 子句(CTE,分解子查询)构建两个“表”。我向note_desc 添加了更多字符串以进行更多测试,包括一个有两个匹配项的字符串。

然后我通过提取最大数字子字符串来“准备”数据,并在note_descidx 列)中跟踪它们的顺序。最后,我将从note_desc 中提取的单个“id”与ids 表中的id 进行比较,最后我对note_desc“表”进行右外连接,为没有添加的字符串添加行有任何匹配。

在您的实际使用中,您可以例如过滤idx = 1(如果您只需要每个字符串的第一个匹配项),并且您可以选择在输出中不显示idx 列。我把它们都放在里面了,所以你看看一切是什么样子的;删除你不需要的。

with 
     note_desc ( str ) as (
       select 'JS 1234 Closed'          from dual union all
       select '123456 May Adjudication' from dual union all
       select 'Closing Bal 5678'        from dual union all
       select '12-3 Bal Adjustment'     from dual union all
       select '1-23-45 Johnson'         from dual union all
       select 'Vendor 123489'           from dual union all
       select '2345'                    from dual union all
       select ''                        from dual union all
       select 'abckm'                   from dual union all
       select '1234-5678 xyz'           from dual
     ),
     ids ( id ) as (
       select 123     from dual union all
       select 1234    from dual union all
       select 5678    from dual union all
       select 12345   from dual union all
       select 123456  from dual union all
       select 1234567 from dual
     ),
     prep ( str, idx, id ) as (
       select str, level, regexp_substr(str, '\d+', 1, level)
       from   note_desc
       connect by regexp_substr(str, '\d+', 1, level) is not null
               and prior str = str
               and prior sys_guid() is not null
     )
select n.str, p.idx, p.id
from                    prep       p
             inner join ids        i  on p.id  = i.id
       right outer join note_desc  n  on n.str = p.str
;

输出(未排序;注意 NULL)

STR                            IDX ID
----------------------- ---------- --------
1234-5678 xyz                    1 1234
1234-5678 xyz                    2 5678
123456 May Adjudication          1 123456
Closing Bal 5678                 1 5678
JS 1234 Closed                   1 1234

Vendor 123489
12-3 Bal Adjustment
1-23-45 Johnson
2345
abckm

11 rows selected.

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多