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

これらのケースに基づいて重複レコードの1つだけを選択するには、「where」を追加する必要があります。

連絡先IDにCTLしかない場合は、連絡先
IDにSTS TPしかない場合は選択し
、連絡先が複数回出現し、''があり、CTLとSTS TPがある場合
は、連絡先にCLがない場合はSTS TPのみを出力するか、 STS TPは、連絡先をまったく出力しません。

WHERE句内にもう1つのケースを追加する必要があると考えていましたが、時間とコードの記述を節約できる別の方法があるかどうか疑問に思いました。

4

4 に答える 4

1

各連絡先の質問IDを提供するCTEを作成します。次に、それをより大きなクエリで使用して、適切な行を選択します。

; 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 に答える
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

要件が投稿したものと異なる場合を除いて、このようなものが必要だと思います。

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 に答える
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 に答える