查找其他列SQL

时间:2018-08-21 18:07:00

标签: sql string search text sas

我尝试在另一列中查找字符串的出现。

如果该字符串出现多次(在连接的列中),那么我想将其删除。

我的数据(问题)摘录见下图。

这是我启动的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列“完整名称空间”将“名字”,“姓氏”和“出生名称”连接起来。

数据不一致,意味着“ first_name”可能包含某个人的名字和姓氏,等等。因此,在连接的列“ full_name_no_space”中有重复的条目,我尝试将其删除。

enter image description here

1 个答案:

答案 0 :(得分:0)

嗯,行不通?

(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) 

这基本上是通过比较来确定是否已经匹配-如果不匹配,则将其包括在结果中。