Need help with a query that returns only rows that meet specific condition

  • Hi there, I know this is probably a basic SQL question,

    Here's what my table looks like;

    UniqID | Code |

    1 | ABC

    1 | 123

    2 | ABC

    3 | ABC

    4 | ABC

    4 | ABC

    I only want to UniqIds that only have the CODE of ABC... and if it contains ANYTHING other than ABC then It doesnt return that UniqID... Now keep in mind there's multiple different codes.. I'm just looking for a bit of code that drops any ID's that don't have my criteria, Any help is appreciated.. thanks!

  • Would something like this work?

    SELECT DISTINCT UniqID

    FROM <table>

    WHERE Code = 'ABC'

    or

    SELECT UniqID, CODE

    FROM <table>

    WHERE Code = 'ABC'

    Edit: I've misunderstood the query, and have updated my answer to something more appropriate.

  • So many ways to do this, that I can't decide the best option.:-D

    Here are 2 ways.

    CREATE TABLE #SampleData (

    UniqID int,

    Code varchar(10)

    );

    INSERT INTO #SampleData

    VALUES

    (1, 'ABC'),

    (1, '123'),

    (2, 'ABC'),

    (3, 'ABC'),

    (4, 'ABC'),

    (4, 'ABC');

    SELECT *

    FROM #SampleData

    WHERE UniqID NOT IN (SELECT UniqID

    FROM #SampleData

    WHERE Code <> 'ABC');

    WITH CTE AS(

    SELECT *,

    MAX( NULLIF( Code, 'ABC')) OVER(PARTITION BY UniqID) InvalidCodes

    FROM #SampleData

    )

    SELECT UniqID, Code

    FROM CTE

    WHERE InvalidCodes IS NULL;

    GO

    DROP TABLE #SampleData

    Luis C.
    General Disclaimer:
    Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

    How to post data/code on a forum to get the best help: Option 1 / Option 2
  • Edit: removed, I think I've misunderstood the question.

  • SELECT UniqID

    FROM #SampleData

    GROUP BY UniqID

    HAVING MIN(Code) = 'ABC' AND MAX(Code) = 'ABC'

    SQL DBA,SQL Server MVP(07, 08, 09) "It's a dog-eat-dog world, and I'm wearing Milk-Bone underwear." "Norm", on "Cheers". Also from "Cheers", from "Carla": "You need to know 3 things about Tortelli men: Tortelli men draw women like flies; Tortelli men treat women like flies; Tortelli men's brains are in their flies".

  • Thank you Everyone!

    Luis! I was trying to add more codes to this section would the syntax look something like this?

    SELECT *

    FROM #SampleData

    WHERE UniqID NOT IN (SELECT UniqID

    FROM #SampleData

    WHERE Code <> 'ABC', 'DEF', 'HIJ');

    Thanks!

  • Scott did my first solution (that one only scans the table once)...

    Here's another way:

    SELECT UniqID

    FROM #SampleData

    WHERE Code = 'ABC'

    EXCEPT

    SELECT UniqID

    FROM #SampleData

    WHERE Code <> 'ABC'

    "I cant stress enough the importance of switching from a sequential files mindset to set-based thinking. After you make the switch, you can spend your time tuning and optimizing your queries instead of maintaining lengthy, poor-performing code."

    -- Itzik Ben-Gan 2001

  • Alan.B (10/22/2015)


    Scott did my first solution (that one only scans the table once)...

    Here's another way:

    SELECT UniqID

    FROM #SampleData

    WHERE Code = 'ABC'

    EXCEPT

    SELECT UniqID

    FROM #SampleData

    WHERE Code <> 'ABC'

    And another:

    SELECT o.UniqID

    FROM #SampleData o

    WHERE o.Code = 'ABC'

    AND NOT EXISTS (

    SELECT 1

    FROM #SampleData i

    WHERE i.UniqID = o.UniqID

    AND i.Code <> 'ABC'

    )

    “Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

    For fast, accurate and documented assistance in answering your questions, please read this article.
    Understanding and using APPLY, (I) and (II) Paul White
    Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden

  • Hmm, I am slightly confused here because as per my understanding this wiil be a simple or rather a basic query to fetch those code having only ABC.

    Is there something that I am missing or is it that I have not understood the question.

  • ranjitdaljitmand (10/23/2015)


    Hmm, I am slightly confused here because as per my understanding this wiil be a simple or rather a basic query to fetch those code having only ABC.

    Is there something that I am missing or is it that I have not understood the question.

    Your understanding is the same as those who have already provided a solution. Are you offering an alternative solution to those already posted? There are a few remaining.

    “Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

    For fast, accurate and documented assistance in answering your questions, please read this article.
    Understanding and using APPLY, (I) and (II) Paul White
    Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden

  • ranjitdaljitmand (10/23/2015)


    Hmm, I am slightly confused here because as per my understanding this wiil be a simple or rather a basic query to fetch those code having only ABC.

    Is there something that I am missing or is it that I have not understood the question.

    You might be thinking on doing something like:

    SELECT *

    FROM #SampleData

    WHERE Code = 'ABC'

    That won't work because it will return rows that have code 'ABC', but also have other rows with same UniqID but different code. That's why it gets a little bit more complicated.

    Luis C.
    General Disclaimer:
    Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

    How to post data/code on a forum to get the best help: Option 1 / Option 2
  • rourrourlogan (10/22/2015)


    Thank you Everyone!

    Luis! I was trying to add more codes to this section would the syntax look something like this?

    SELECT *

    FROM #SampleData

    WHERE UniqID NOT IN (SELECT UniqID

    FROM #SampleData

    WHERE Code <> 'ABC', 'DEF', 'HIJ');

    Thanks!

    As you should know, a comparison operator can only have one value in each side of it. For that, you need to use something else. Check this which will show you more detail of what you need: https://msdn.microsoft.com/en-us/library/ms177682.aspx

    By the way, we're volunteers here and if we don't answer in the forum is probably because we have other things to do. Please avoid sending private messages to ask questions that should go in the forum.

    Luis C.
    General Disclaimer:
    Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

    How to post data/code on a forum to get the best help: Option 1 / Option 2

Viewing 12 posts - 1 through 11 (of 11 total)

You must be logged in to reply to this topic. Login to reply