SQL Server的案例和输出

时间:2013-02-13 17:24:28

标签: sql sql-server-2008

我需要从包含多个表格的案例中选择某些记录,我需要根据我将要创建的一些案例来选择它们。

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

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

我需要添加一个“where”,根据这些情况只选择一个重复记录:

如果contactid只有CTL,那么选择那个 如果contactid只有STS TP,那么选择那个 如果一个联系人出现不止一次并且它有''和CTL和STS TP,那么只输出STS TP
如果联系人没有CL或STS TP,则根本不输出联系人。

我在想我需要在WHERE子句中添加一个案例,但我想知道是否有另一种方法可以节省我的时间和代码编写

4 个答案:

答案 0 :(得分: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 <> ''

答案 1 :(得分: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

答案 2 :(得分:1)

创建一个CTE,为每个联系人提供问题ID。然后在较大的查询中使用它来选择适当的行。

; 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

答案 3 :(得分: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'