Select all data where one condition is met

  • Below is some sample data. I want to SELECT all Stat's where an ID exists with a StatDate in AppRec >= 2012-12-01. So the result should be:

    ID Stat StatDate

    1 AppRec 2012-12-01

    2 AppCom 2012-12-02

    Note: ID 3 shouldn't get returned because although his "AppCom" Stat is after 2012-12-01, his "AppRec" wasn't. Also note that I need to return all statuses (both AppRec and AppCom in this case). So a WHERE Stat = 'AppRec" doesn't work. Any help is appreciated!

    /*Create the table*/

    CREATE TABLE Apps

    (

    IDVARCHAR(50)NOT NULL,

    StatVARCHAR(50)NOT NULL,

    StatDateDATENOT NULL

    )

    ;

    /*Putting data in the table to create the test enviroment*/

    INSERT INTO Apps

    (

    ID,

    Stat,

    StatDate

    )

    Values

    ('1', 'AppRec', '2012-12-01'),

    ('1', 'AppCom', '2012-12-02'),

    ('2', 'AppRec', '2012-11-01'),

    ('2', 'AppCom', '2012-11-01'),

    ('3', 'AppRec', '2012-11-15'),

    ('3', 'AppCom', '2012-12-01')

    ;

  • Is there a typo in your question? It looks like you want the following to be returned, unless I have misunderstood:

    ID Stat StatDate

    1 AppRec 2012-12-01

    1 AppCom 2012-12-02

    If so, this will do it:

    SELECT ID, Stat, StatDate

    FROM Apps

    WHERE ID IN (SELECT ID FROM Apps WHERE Stat = 'AppRec' AND StatDate >= '2012-12-01')

  • Your typo messed me up too...

    You can also try:

    ;WITH x AS

    (SELECT ID, Stat, StatDate

    FROM #Apps

    WHERE Stat='AppRec' AND StatDate >= '2012-12-01' )

    SELECT ID, Stat, StatDate

    FROM #Apps

    WHERE ID in (SELECT ID FROM x)

    Edit: I just realized I posted basically the exact same query as above which will produce the same query plan... Now you have the CTE and non-CTE-version... :smooooth:

    "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

  • I really apologize for the typo! Typo notwithstanding, you guys gave me exactly what I was looking for. Thanks alot for the help!

  • Again this is really helpful. I hope you all won't mind a follow-up question:

    What is the difference between...

    WHERE [column] IN ([subquery])

    and

    WHERE EXISTS ([subquery])

    ?

    I've been trying to read on it and trying queries on my test server but can't come up with anything helpful. Would someone mind explaining? I want to be able to support my code 🙂

  • DataAnalyst011 (12/17/2012)


    Again this is really helpful. I hope you all won't mind a follow-up question:

    What is the difference between...

    WHERE [column] IN ([subquery])

    and

    WHERE EXISTS ([subquery])

    ?

    I've been trying to read on it and trying queries on my test server but can't come up with anything helpful. Would someone mind explaining? I want to be able to support my code 🙂

    In the first case, the WHERE clause is true if the [column] exists in any of the rows returned by the subquery.

    In the second case, the WHERE clause is true if any rows are returned by the subquery.


    My mantra: No loops! No CURSORs! No RBAR! Hoo-uh![/I]

    My thought question: Have you ever been told that your query runs too fast?

    My advice:
    INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
    The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.

    Need to UNPIVOT? Why not CROSS APPLY VALUES instead?[/url]
    Since random numbers are too important to be left to chance, let's generate some![/url]
    Learn to understand recursive CTEs by example.[/url]
    [url url=http://www.sqlservercentral.com/articles/St

  • Here is the code using the EXISTS in the WHERE clause:

    /*Create the table*/

    CREATE TABLE dbo.Apps

    (

    IDVARCHAR(50)NOT NULL,

    StatVARCHAR(50)NOT NULL,

    StatDateDATENOT NULL

    )

    ;

    /*Putting data in the table to create the test enviroment*/

    INSERT INTO dbo.Apps

    (

    ID,

    Stat,

    StatDate

    )

    Values

    ('1', 'AppRec', '2012-12-01'),

    ('1', 'AppCom', '2012-12-02'),

    ('2', 'AppRec', '2012-11-01'),

    ('2', 'AppCom', '2012-11-01'),

    ('3', 'AppRec', '2012-11-15'),

    ('3', 'AppCom', '2012-12-01')

    ;

    go

    select

    a1.ID,

    a1.Stat,

    a1.StatDate

    from

    dbo.Apps a1

    where exists(select

    1

    from

    dbo.Apps a2

    where

    a1.ID = a2.ID and

    a2.Stat = 'AppRec' and

    a2.StatDate >= '20121201');

    go

    drop table dbo.Apps;

    go

Viewing 7 posts - 1 through 6 (of 6 total)

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