简体   繁体   中英

Match the words in two columns oracle sql by breaking the string and comparing each word

I have two columns in my table for instance column_A and column_B

Suppose column_A has data "best buy card credit" and column_B has "credit no take buy order", here i need to match the words in both the columns and return the number of matching words . In this case it should return 2 as "buy" and "credit" match. Can anyone please suggest sql code to do the same.

Please NOTE : size of column_a and column_b is not fixed ie the number of words in both might change.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM