0

我需要从多个表中选择带有案例的某些记录,并且我需要根据我将创建的某些案例来选择它们。

这是我到目前为止的查询:

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 子句中再添加一个案例,但我想知道是否有另一种方法可以节省我的时间和代码编写

4

4 回答 4

1

我认为您需要更多类似的东西,除非您的要求与您发布的不同:

SELECT
    datecomplete, ownerIdfk, finalScore, surveyid, surveyname, contactid
    CASE WHEN max_questionSort = 98 THEN 'Save the Sale TP' ELSE  'Close the Loop' END AS questionTag,
    c.contactid AS contactid, c.FIRST, c.LAST,
    cc.clientcontactid, cc.leadsource,
    se.eventdate, se.ProcedureName, se.LocationName, se.salesperson, se.SpecialistName
FROM
    (SELECT 
        datecomplete, ownerIdfk, finalScore, surveyid, surveyname, contactid
        MAX(QuestionSort) AS max_questionSort 
    FROM
        (SELECT 
            CASE 
                    WHEN questionidfk = 98 THEN 1
                    WHEN questionidfk = 99 THEN 0
            END AS QuestionSort ,
            datecomplete, st.ownerIdfk, finalScore, surveyid, surveyname, contactid
        FROM 
            sigweb.dbo.survey_tracking st INNER  JOIN 
            survey.dbo.results r ON 
            r.owneridfk = st.owneridfk
         WHERE
            questionidfk IN (98,99)) w 
    GROUP BY
        datecomplete, ownerIdfk, finalScore, surveyid, surveyname, contactid) st INNER JOIN 
        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
于 2013-02-13T18:55:49.240 回答
1

尝试这个。

;with cte as (
 select QuestionTag =( CASE 
                        WHEN qt.id = 98 THEN 'Save the Sale TP'
                        WHEN qt.id = 99 THEN 'Close the Loop'
                        ELSE ''
                      END) ,
                     --Other_List_of_Columns,
                     row_number() over(partition by c.contactid order by 
                       case when qt.id = 98 then 1 
                            when qt.id = 99 then 2
                       else 3 end) rn
  From your_list_of_tables
)
select top (10) * from cte where rn = 1 and QuestionTag <> ''
于 2013-02-13T17:51:31.180 回答
1

Create a CTE that provides the question id's for each contact. Then use that in your larger query to pick the appropriate rows.

; with ContactQuestions as (
  select ContactId, QuestionId,
    case when qt.id = 98 then 1 else 0 end as STSTP,
    case when qt.id = 99 then 1 else 0 end as CTL
    from many_joined_tables ),
  ...

where case
  -- Only STSTP.
  when STSTP = 1 and not exists ( select 42 from ContactQuestions where ContactId = c.ContactId and CTL = 1 ) then 1
  -- Only CTL.
  when CTL = 1 and not exists ( select 42 from ContactQuestions where ContactId = c.ContactId and STSTP = 1 ) then 1
  -- Multiple rows for a contact and they have CTL and STSTP then output the STSTP row.
  --   Since only CTL or STSTP can be set for a single row this is equivalent to checking for
  --   any CTL row and the current row being STSTP.
  when exists ( select 42 from ContactQuestions where ContactId = c.ContactId and CTL = 1 ) and STSTP = 1 then 1
  else 0
  end = 1
于 2013-02-13T19:07:18.390 回答
0

这是我改进的尝试。它做了它应该做的事情,但它有点慢。

        select 
        qt.questiontag, qt.id,
        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 and st.surveyid = se.surveytypeidfk
        left join survey.dbo.questiontags qt on 
            (select top 1 qa.tagidfk
            from survey.dbo.results r
            join survey.dbo.questionsadmin qa on r.answeridfk = qa.answeridfk and (qa.tagidfk = 98 or qa.tagidfk = 99)
            where st.owneridfk = r.owneridfk
            order by qa.tagidfk ) = qt.id
        where st.datecomplete > '2013-02-01'
于 2013-02-13T21:08:22.477 回答