我需要从多个表中选择带有案例的某些记录,并且我需要根据我将创建的某些案例来选择它们。
这是我到目前为止的查询:
select distinct top 10
QuestionTag =( CASE
WHEN qt.id = 98 THEN 'Save the Sale TP'
WHEN qt.id = 99 THEN 'Close the Loop'
WHEN qt.id != 98 AND qt.id != 99 THEN ''
WHEN qt.id = 98 AND qt.id = 99 THEN 'Save the Sale TP'
END),
c.contactid as contactid, c.first, c.last,
st.datecomplete, st.ownerIdfk, st.finalScore, st.surveyid,
cc.clientcontactid, cc.leadsource,
se.eventdate, se.ProcedureName, se.LocationName, se.salesperson, se.SpecialistName, st.surveyname
from sigweb.dbo.survey_tracking st
join sigweb.dbo.survey_types_main stm on st.surveyid = stm.surveyidfk
join sigweb.dbo.contact c on st.contactid = c.contactid
join survey.dbo.client_contacts cc on c.contactid = cc.contactidfk
join survey.dbo.survey_events se on c.contactid = se.contactidfk
join survey.dbo.results r on r.owneridfk = st.owneridfk
left join survey.dbo.questionsAdmin qa on qa.questionidfk = r.questionidfk
join survey.dbo.QuestionTags qt on qt.id=qa.tagidfk
这是输出的一部分:
QuestionTag ContactID First Last
----------- --------- ----- ----
2012082911569010000001 John Reardon
Close the Loop 2012082911569010000001 John Reardon
Save the Sale TP 2012082911569010000001 John Reardon
2012082911569010000003 Beverly Conley
Close the Loop 2012082911569010000003 Beverly Conley
Save the Sale TP 2012082911569010000003 Beverly Conley
2012082911569010000009 Larry Grigsby
2012082911569010000011 Brenda Bain
2012082911569010000012 Richard Pecora
2012082911569010000018 Karl Oliszczak
我需要添加一个“位置”来根据这些情况只选择一个重复记录:
如果一个contactid只有CTL然后选择
如果一个contactid只有STS TP然后选择
如果一个联系人出现不止一次并且它有''并且CTL和STS TP然后只输出STS TP
如果一个联系人没有CL或STS TP 然后根本不输出触点。
我在想我需要在 WHERE 子句中再添加一个案例,但我想知道是否有另一种方法可以节省我的时间和代码编写