[英]Split table into related tables based on regex pattern matching of a text column
假設我們有一個產品表,其中每個供應商對同一產品的名稱略有不同。 使用正則表達式模式匹配來分割表以識別我們相似的字符串的最佳方法是什么。
請參閱以下示例:
p_id p_name cat start_time vendor attrs
-------------------------------------------------------------------------------------------
1 'iphone' 'phones' some date 'google' some_jsonb
2 'apple iphone' 'phones' some date 'ebay' some_other_jsonb
想要的結果:
table A
p_id p_name
1 'iphone'
table B
e_id p_ref vendor attrs
1 1 'google' some_jsonb
2 1 'ebay' some_other_jsonb
我的問題不在於正則表達式規則或將表拆分為兩個相關表,而是在哪里以及如何實現我的正則表達式規則以在列 p_name 中查找重復項?
如果對於每個p_name
,您想要表中已經存在的最短子名,那么您可以使用如下邏輯:
select e.p_name
from example e
where not exists (select 1
from example e2
where e.p_name like '%' || e2.p_name || '%' and
e.p_name <> e2.pname
);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.