Oracle SQL 中搜索字符串中的多个单词并以串联方式返回所有匹配的单词

问题描述 投票:0回答:1

我有一个单词列表:

紧急、令人失望、生气、无法使用、升级、不满意

我的表格如下: 基本包含反馈。

句子ID 文字
1 这是一次又一次发生的令人失望的问题。请紧急解决这个问题
2 对服务不满意。升级案件

我想要像下面这样的东西

句子ID 文字 列表中的单词
1 这是一次又一次发生的令人失望的问题。请紧急解决这个问题 令人失望,紧急
2 对服务不满意。升级案件 不满意,升级
sql oracle
1个回答
0
投票

一个简单的选项包括单词列表和句子之间的交叉连接、

instr
函数的使用以及每个句子所有匹配的聚合。

样本数据:

SQL> with
  2  words (word) as
  3    (select 'Urgent'        from dual union all
  4     select 'Disappointing' from dual union all
  5     select 'Pissed off'    from dual union all
  6     select 'Unavailable'   from dual union all
  7     select 'Escalate'      from dual union all
  8     select 'Dissatisfied'  from dual
  9    ),
 10  sentences (id, text) as
 11    (select 1, 'This is disappointing issue occurring again and again. Please fix this urgent' from dual union all
 12     select 2, 'Dissatisfied with service. Escalate the case'                                  from dual
 13    )

查询从这里开始:

 14  select s.id, s.text,
 15    listagg(case when instr(upper(s.text), upper(w.word)) > 0 then w.word end, ', ')
 16      within group (order by w.word) as match
 17  from sentences s cross join words w
 18  group by s.id, s.text;

        ID TEXT                                     MATCH
---------- ---------------------------------------- ----------------------------------------
         1 This is disappointing issue occurring ag Disappointing, Urgent
           ain and again. Please fix this urgent

         2 Dissatisfied with service. Escalate the  Dissatisfied, Escalate
           case


SQL>

请注意,如果例如单词包含“the”,并且句子包含“Theodore”,则此方法会返回 误报。但是,看看您发布的内容,情况可能并非如此,因为您要查找的单词非常独特

© www.soinside.com 2019 - 2024. All rights reserved.