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

  • Home
  • SEARCH
  • 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 6368643
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T00:42:06+00:00 2026-05-25T00:42:06+00:00

I am trying to do something similar to How can I improve this SQL

  • 0

I am trying to do something similar to
How can I improve this SQL query?

However, in there logic
Essentially I have a list of IDs that could have multiple values associated with them, the values being Yes, No, or some other string. For ID x, if any of the values are a Yes, x should be Yes, if they are all No, it should be No, if they contain any other values but yes and no, display that value. I only want to return 1 row per ID, no duplicates.

but in my logic if all values are yes, then display yes, if all values are no then display no, if it is a mix of yes,no or anything else display mix

DECLARE @tempTable table ( ID int, Val varchar(1) )

    INSERT INTO @tempTable ( ID, Val ) VALUES ( 10, 'Y')
    INSERT INTO @tempTable ( ID, Val ) VALUES ( 11, 'N')
    INSERT INTO @tempTable ( ID, Val ) VALUES ( 11, 'N')
    INSERT INTO @tempTable ( ID, Val ) VALUES ( 12, 'Y')
    INSERT INTO @tempTable ( ID, Val ) VALUES ( 12, 'Y')
    INSERT INTO @tempTable ( ID, Val ) VALUES ( 12, 'Y')
    INSERT INTO @tempTable ( ID, Val ) VALUES ( 13, 'N')
    INSERT INTO @tempTable ( ID, Val ) VALUES ( 14, 'Y')
    INSERT INTO @tempTable ( ID, Val ) VALUES ( 14, 'N')
    INSERT INTO @tempTable ( ID, Val ) VALUES ( 15, 'Y')
    INSERT INTO @tempTable ( ID, Val ) VALUES ( 16, 'Y')
    INSERT INTO @tempTable ( ID, Val ) VALUES ( 17, 'F')
    INSERT INTO @tempTable ( ID, Val ) VALUES ( 18, 'P')


    SELECT DISTINCT t.ID, COALESCE(t2.Val, t3.Val, t4.Val)
    FROM @tempTable t
    LEFT JOIN
    (
        SELECT ID, Val
        FROM @tempTable
        WHERE Val = 'Y'
    ) t2 ON t.ID = t2.ID
    LEFT JOIN
    (
        SELECT 
        ID, Val FROM @tempTable
        WHERE Val = 'N'
    ) t3 ON t.ID = t3.ID
    LEFT JOIN
    (
        SELECT ID, Val
        FROM @tempTable
        WHERE Val <> 'Y' AND Val <> 'N'
    ) t4 ON t.ID = t4.ID

Update dbo.households
SET dbo.households.code = #TempTable.code
FROM #TempTable
WHERE dbo.households.id = #TempTable.id 
  • 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-05-25T00:42:07+00:00Added an answer on May 25, 2026 at 12:42 am
    SELECT  ID ,
            CASE WHEN MAX(Val) = MIN(Val)
                      AND MAX(Val) IN ( 'N', 'Y' ) THEN MAX(Val)
                 ELSE 'M'
            END
    FROM    @tempTable
    GROUP BY ID
    

    Or a version that returns Y if any exist rather than if all are Y

    SELECT  ID ,
            ISNULL(MAX(CASE WHEN Val = 'Y' THEN 'Y' END), 
                   CASE WHEN MAX(Val) = MIN(Val) THEN MAX(Val)
                                  ELSE 'M'
                             END)
    FROM    @tempTable
    GROUP BY ID
    

    Or a final version that takes literally the “if they contain any other values but yes and no, display that value.” statement.

    SELECT  ID ,
            ISNULL(MAX(CASE WHEN Val = 'Y' THEN 'Y'END), 
                   ISNULL(MAX(CASE WHEN Val NOT IN ( 'N', 'Y' ) THEN Val END), 
                          MAX(Val)))
    FROM    @tempTable
    GROUP BY ID
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write something similar to this (sorry if the sample is not
This is what I'm trying to do something similar to the following request, but
I'm trying to insert a comment character into a string something similar to this:
I am trying to create something similar to the OS X finder. How can
I'm trying to do something similar to this post where I don't pull back
I'm trying to implement something similar to a cart where you can drop items
I'm trying to mimic something similar to FB. Basically, users can post comments in
I am trying to implement something similar to the flight control game. There will
I am trying to do something similar to http://www.winktoolkit.org/previews/63/ As you can see from
I am trying to do something similar to what the user who asked this

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.