【问题标题】:Oracle SQL compare strings and find matching sub-stringsOracle SQL 比较字符串并找到匹配的子字符串
【发布时间】:2020-12-06 22:32:10
【问题描述】:

我有与两个表中的两个不同实体相关联的冒号分隔标签。想要对标签进行子字符串匹配并关联实体。

Table 1 - Table of issues

Issue ---------------- Tag

Issue 1 -------------- Dual UOM:Serial Control:Material Issue

Issue 2 -------------- Validity rule:Effectivity date


Table 2 - Table of Tests


Test ----------------- Tag

Test 1 --------------- Inventory:Outbound:Material Issue

Test 2 --------------- Items:Single UOM

Test 3 --------------- Items:Dual UOM

Test 4 --------------- Recipe:Validity Rule

Test 5 --------------- Formula:Version control:date

Test 6 --------------- Formula:Effectivity date

现在,对于表 1 中的每个问题,我需要将其关联标签与表 2 中的标签进行比较,并找到适用的测试。

在上面的例子中,

问题 1 - 匹配的测试将是测试 1、测试 3

问题 2 - 匹配的测试将是测试 4、测试 5

与问题和测试相关的所有标签都来自一个通用的标签主机。

非常感谢提供 sql 代码 sn-p 以执行此子字符串到子字符串匹配的任何帮助。

【问题讨论】:

  • 嗨,你试过什么?如果你放一些代码来展示你的方法,你会得到更多的答案。
  • 关系模型的重点是避免多值字段。如果您无法在规范化表中定义此类数据,我不确定您为什么要使用 Oracle。

标签: sql oracle substring match


【解决方案1】:

谢谢,这行得通 我确实将标签分成几行,然后使用 substr,instr 匹配。

【讨论】:

    【解决方案2】:

    这里有一个选项:使用INSTR 函数将问题分成几行并将它们与测试标签进行比较。请注意,字母大小写必须匹配。如果不是(实际上),请使用lower 或upper 函数。

    在代码中读取 cmets(分为几个部分,以提高可读性)。


    先采样数据:

    SQL> with
      2  -- sample data
      3  issues (issue, tag) as
      4    (select 1, 'Dual UOM:Serial Control:Material Issue' from dual union all
      5     select 2, 'Validity Rule:Effectivity date'         from dual
      6    ),
      7  tests (test, tag) as
      8    (select 1, 'Inventory:Outbound:Material Issue' from dual union all
      9     select 2, 'Items:Single UOM'                  from dual union all
     10     select 3, 'Items:Dual UOM'                    from dual union all
     11     select 4, 'Recipe:Validity Rule'              from dual union all
     12     select 5, 'Formula:Version control:date'      from dual union all
     13     select 6, 'Formula:Effectivity date'          from dual
     14    ),
    

    将问题拆分成行 (splitiss),将它们与测试标签 (temp) 进行比较

     15  -- split issues into rows ...
     16  splitiss as
     17    (select issue,
     18            tag,
     19            regexp_substr(tag, '[^:]+', 1, column_value) val
     20     from issues cross join
     21       table(cast(multiset(select level from dual
     22                           connect by level <= regexp_count(tag, ':') + 1
     23                          ) as sys.odcinumberlist))
     24    ),
     25  -- ... and now compare them to test tags
     26  temp as
     27    (select i.issue, i.tag issue_tag, i.val, t.test, t.tag test_tag,
     28       instr(t.tag, i.val) ins
     29     from splitiss i cross join tests t
     30    )
    

    返回结果:

     31  -- return only test tags which match to separate issues (INS > 0)
     32  select t.issue,
     33         t.issue_tag,
     34         listagg(t.test, ', ') within group (order by t.test) matched_tests
     35  from temp t
     36  where t.ins > 0
     37  group by t.issue, t.issue_tag;
    
         ISSUE ISSUE_TAG                              MATCHED_TESTS
    ---------- -------------------------------------- --------------------
             1 Dual UOM:Serial Control:Material Issue 1, 3
             2 Validity Rule:Effectivity date         4, 6
    
    SQL>
    

    附:我相信您为问题 #2 发布了错误的测试标签;应该是4, 6,而不是4, 5。

    【讨论】:

      猜你喜欢
      • 2015-07-31
      • 2015-06-17
      • 2015-11-19
      • 1970-01-01
      • 2020-08-23
      • 1970-01-01
      • 2022-03-30
      • 1970-01-01
      • 2011-11-09
      相关资源
      最近更新 更多