通过打破字符串并比较每个单词来匹配oracle sql两列中的单词

时间:2013-09-12 07:12:02

标签: sql oracle

我的表格中有两列,例如column_Acolumn_B

假设column_A有数据"最佳购买信用卡"并且column_B已经"信用不接受购买订单", 在这里,我需要匹配两列中的单词并返回匹配单词的数量。 在这种情况下,它应该返回2作为"购买"和"信用"比赛。 任何人都可以建议sql代码来做同样的事情。

请注意column_acolumn_b的大小不固定,即两者中的字数可能会发生变化。

1 个答案:

答案 0 :(得分:3)

with t as (
SELECT 1 AS ID, 'best buy card credit' column_1,
       'credit no take buy order' column_2
  FROM DUAL
UNION ALL
SELECT 2 AS ID, 'gaurav is  fool' column_1, 'saurabh is fool' column_2
  FROM DUAL
           )
,t1_column1 as (
SELECT     ID, LEVEL AS n, REGEXP_SUBSTR (column_1, '[^ ]+', 1, LEVEL) AS val
      FROM t
CONNECT BY REGEXP_SUBSTR (column_1, '[^ ]+', 1, LEVEL) IS NOT NULL
        )
,t1_column2 as (
SELECT     ID, LEVEL AS n, REGEXP_SUBSTR (column_2, '[^ ]+', 1, LEVEL) AS val
      FROM t
CONNECT BY REGEXP_SUBSTR (column_2, '[^ ]+', 1, LEVEL) IS NOT NULL
        )

select id, LISTAGG(VAL,',') WITHIN GROUP(ORDER BY VAL ) words ,COUNT(*) "total matched words"
from
(
SELECT DISTINCT t1_column1.ID ID, t1_column1.val val
           FROM t1_column1, t1_column2
          WHERE t1_column1.ID = t1_column2.ID
            AND t1_column1.val = t1_column2.val
)
group by id