Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9296219
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T21:47:09+00:00 2026-06-18T21:47:09+00:00

I need to select certain records with cases from multiple tables and I need

  • 0

I need to select certain records with cases from multiple tables and I need to select them based on some cases that I will create.

This is what I have so far for my query:

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

This is part of the output:

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

I need to add a “where” to select only one of the duplicate records based on these cases:

IF a contactid has only CTL then select that
IF a contactid has only STS TP then select that
IF a contact appears more than once and it has ” and CTL and STS TP then output only the STS TP
IF a contact does not have CL or STS TP then do not output the contact at all.

I was thinking that I need to add one more case inside my WHERE clause but I was wondering if there is another method that will save me time and code writing

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T21:47:10+00:00Added an answer on June 18, 2026 at 9:47 pm

    This is my improved attempt. It does what it is supposed to do but it is kind of slow.

            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'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to select all records from my SQL Server database that falls between
I need to select records from 2 tables, one called cities and one called
I need to select elements of a certain class except children of $(this). The
I need to delete an option from a select in certain circumstances. Basically: if(mystatement
I need to select only 20 records from the table and than add pagination
I need to build an SQL statement to delete from certain table the records
I need to perform a delta select on a table (return only records that
I need to select all from table1 where active=1 and replace t1cid,t2cid,L1cid,L2cid---L10cid with corresponding
I would like to delete records which are considered duplicates based on them having
i want something like this: SELECT DISTINCT ID from copies WHERE timestamp < 1229444506

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.