将case字段中的字段连接成1

时间:2017-06-13 21:28:47

标签: sql sql-server

我有这个查询来查找所有具有' nan'进入它并且必须通过记录显示哪些字段具有此值记录。任何建议

select Lastname, Firstname, Middle_Name__c ,MailingCity,Mailingstreet,Email,Parent1_FirstName,Parent1_LastName,Parent1_Email,  Parent2_FirstName,Parent2_LastName, parent2_email,
      case
       when  LastName like '%nan%'  then LastName  else ' 'end 'lastname_Imapcted'
      ,case when  FirstName like '%nan%' then FirstName else ' '  end 'Firstname_Imapcted'
      ,case when Middle_Name__c like '%nan%' then  Middle_Name__c else ' ' end 'MiddleName_Imapcted' 
      ,case when  MailingCity like '%nan%'  then MailingCity else ' ' end 'MailingCity_Imapcted'
  ,case when  Email like '%nan%' then  Email else ' ' end 'Email_Imapcted'
,case  when  Parent1_FirstName like '%nan%'then  Parent1_FirstName else ' ' end 'Parent1_Firstname_Imapcted'
 ,case when  Parent1_LastName like '%nan%' then Parent1_LastName else ' ' end 'Parent1lastname_Imapcted'
 ,case when  Parent1_Email like'%nan%' then  Parent1_email else ' ' end 'Parent1email_Imapcted'
 ,case when  Parent2_Email like '%nan%' then Parent2_email else ' ' end 'Parent2email_Imapcted'
 ,case when  Parent2_FirstName like '%nan%' then Parent2_firstname else ' ' end 'Parent2firstname_Imapcted'
 ,case when   Parent2_LastName like '%nan%'then  parent2_lastname else ' ' end 'Parent2lastname_Imapcted '
 ,case when   MailingStreet like '%nan%' then  MailingStreet else ' ' end 'Mailingstreet_Imapcted'
 ,concat(Parent1_Firstname_Imapcted,Parent2lastname_Imapcted) as 'Impacted Fields'
   from RE_Data_Sandbox..Temp_ADB_Student_Roster_ALL_06092017
     where LastName like '%nan%'
 or FirstName like '%nan%'
 or Middle_Name__c like '%nan%'
 or MailingCity like '%nan%'
 or Email like '%nan%'
 or Parent1_FirstName like '%nan%'
 or Parent1_LastName like '%nan%'
 or Parent1_Email like'%nan%'
 or Parent2_Email like '%nan%'
 or Parent2_FirstName like '%nan%'
 or Parent2_LastName like '%nan%'
 or MailingStreet like '%nan%'

2 个答案:

答案 0 :(得分:1)

首先将where子句重写为

where concat(LastName,FirstName,Middle_Name__c,MailingCity,Email,Parent1_FirstName,Parent1_LastName,Parent1_Email,Parent2_Email,Parent2_FirstName,Parent2_LastName,MailingStreet) like '%nan%'

您的选择字段应该可以正常使用或更好地使用IIF

IIF ( LastName like '%nan%',LastName,'') 'lastname_Imapcted'
,IIF(  FirstName like '%nan%',FirstName,'')'Firstname_Imapcted'
,IIF( Middle_Name__c like '%nan%', Middle_Name__c ,'') 'MiddleName_Imapcted' 
,IIF(  MailingCity like '%nan%',MailingCity ,'') 'MailingCity_Imapcted'
  ,IIF(  Email like '%nan%', Email ,'') 'Email_Imapcted'
,IIF  Parent1_FirstName like '%nan%'then  Parent1_FirstName ,'') 'Parent1_Firstname_Imapcted'
 ,IIF(  Parent1_LastName like '%nan%',Parent1_LastName ,'') 'Parent1lastname_Imapcted'
 ,IIF(  Parent1_Email like'%nan%', Parent1_email ,'') 'Parent1email_Imapcted'
 ,IIF(  Parent2_Email like '%nan%',Parent2_email ,'') 'Parent2email_Imapcted'
 ,IIF(  Parent2_FirstName like '%nan%',Parent2_firstname ,'') 'Parent2firstname_Imapcted'
 ,IIF(   Parent2_LastName like '%nan%'then  parent2_lastname ,'') 'Parent2lastname_Imapcted '
 ,IIF(   MailingStreet like '%nan%', MailingStreet ,'') 'Mailingstreet_Imapcted'

答案 1 :(得分:0)

您只需连接字符串即可获得字段列表。如果你想要一个以逗号分隔的列表:

select stuff((case when LastName like '%nan%' then ',LastName' else '' end) + 
             (case when FirstName like '%nan%' then ',FirstName' else '' end) + 
             (case when Middle_Name__c like '%nan%' then ',Middle_Name__c' else '' end) + 
             (case when MailingCity like '%nan%' then ',MailingCity' else '' end) + 
             . . .
             (case when MailingStreet like '%nan%' then ',MailingStreet' else '' end),
             1, 1, '')
. . .

stuff()只处理逗号,因此列表看起来更多"正常"。