简化许多类似的陈述

时间:2017-09-13 15:27:57

标签: pervasive

我有一个公司名称或个人姓名的名单。我必须手动决定它是公司还是个人。我查看了每一行并用一个案例来决定。如果它是一家公司,那就是一个人。如果是人,则放置零。此外,我必须将个别名称解析为第一个和最后一个列。每个语句有20行。似乎普遍无法处理所有陈述。我做错了吗?有更简单的方法吗?

实施例

Own_name Dollar Tree LLC Jacob Smith New Life Co Johnson, Robert

以下是SQL的示例:

select CASE WHEN own_name LIKE '%co%' OR own_name LIKE '%LLC% THEN 1 END AS is_compan, // if the own_name has Co or LLC, place a 1 in a column

CASE
    WHEN own_name NOT LIKE '%co%' 
    OR own_name NOT LIKE '%LLC%
    THEN 0 
    END AS is_individual, //if the own_name DOES NOT have Co or LLC, place a 0 in a column

CASE WHEN locate(',', own_name)>0 AND own_name NOT LIKE '%co%' AND own_name NOT LIKE '%LLC%' THEN ltrim(left(own_name, locate(',', own_name)-1))

   WHEN locate(' ', own_name)>0
   AND own_name NOT LIKE '%co%' 
   AND own_name NOT LIKE '%LLC%'
   THEN SUBSTRING(own_name, locate(' ', own_name) + 1, 8000) //if the own_name has a comma or a space and does not have CO or LLC in the column, parse the name for the last name
   END AS LAST_NAME

CASE WHEN locate(',', own_name)>0 AND not locate('-', own_name)<0 AND own_name NOT LIKE '%co%' AND own_name NOT LIKE '%LLC%' THEN ltrim(left(own_name, locate(',', own_name)-1))

   WHEN locate(',', own_name)=0
   AND own_name NOT LIKE '%corp%' 
   AND own_name NOT LIKE '%LLC%' 
   THEN Ltrim(SubString(own_name,1,Isnull(Nullif(locate(' ',own_name),0),1000))) //if the own_name has a comma or a space and does not have CO or LLC in the column, parse the name for the first name
   END AS First_name

结果

Own_name is_compan is individual Last name First name Dollar Tree LLC 1
Jacob Smith 0 Smith Jacob New Life Co 1
Johnson, Robert 0 Johnson Robert

0 个答案:

没有答案