![](/img/trans.png)
[英]Oracle: Comparing string columns from two different table without a primary key to find match/unmatched string
[英]Match the words in two columns oracle sql by breaking the string and comparing each word
我的表中有兩列,例如column_A
和column_B
假設column_A
有數據“best buy card credit”, column_B
有“credit no take buy order”,這里我需要匹配兩列中的單詞並返回匹配單詞的數量。 在這種情況下,它應該返回 2 作為“購買”和“信用”匹配。 任何人都可以建議 sql 代碼來做同樣的事情。
請注意: column_a
和column_b
大小不是固定的,即兩者中的字數可能會改變。
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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.