[英]Find occurrence of column (string) in other column SQL
我嘗試在另一列中查找字符串的出現。
如果該字符串多次出現(在連接的列中),那么我想將其刪除。
我的數據(問題)摘錄見下圖。
這是我開始的SQL。
SELECT
t1.FIRST_NAME as FIRST_NAME,
t1.LAST_NAME as LAST_NAME,
t1.BIRTH_NAME as BIRTH_NAME,
compress(t1.FIRST_NAME) || compress(t1.LAST_NAME) || compress(t1.BIRTH_NAME) as full_name_no_space
FROM
atable t1
列4“ full_name_no_space”連接“ first_name”,“ last_name”和“ birth_name”。
數據不一致,意味着“ first_name”可以包含某人的名字和姓氏等。因此,在連接的列“ full_name_no_space”中存在重復的條目,我嘗試將其刪除。
嗯,這行得通嗎?
(case when compress(t1.first_name) like '%' || compress(t2.last_name) || '%' and
t1.first_name <> t2.last_name
then ''
when compress(t1.first_name) like '%' || compress(t2.birth_name) || '%' and
t1.first_name <> t2.birth_name
then ''
else compress(t1.first_name)
end) ||
(case when compress(t1.last_name) like '%' || compress(t2.birth_name) || '%' and
t1.first_name <> t2.birth_name
then ''
when compress(t1.last_name) like '%' || compress(t2.first_name) || '%'
then ''
else compress(t1.last_name)
end) ||
(case when compress(t1.birth_name) like '%' || compress(t2.first_name) || '%'
then ''
when compress(t1.birth_name) like '%' || compress(t2.last_name) || '%'
then ''
else compress(t1.birth_name)
end)
基本上,這是通過比較來確定是否已經匹配-如果不匹配,則將其包括在結果中。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.