SELECT WHERE NOT IN query

  • This isn't a problem anymore (it almost was, I was 90% of the way done with a forum post when I figured it out), but due to how weird/interesting it is, I decided to post it anyways.

    I wrote a very simple query:

    SELECT DISTINCT C.Cust_Num

    FROM Customer C

    WHERE C.Cust_Num NOT IN (SELECT DISTINCT Cust_Num FROM fs_Unit)

    I was expecting approximately 12k rows. I got... none.

    The outer query:

    SELECT DISTINCT C.Cust_Num FROM Customer C

    Returns 21K rows.

    The NOT IN query alone:

    SELECT DISTINCT Cust_Num FROM fs_Unit

    Returns 9K rows.

    Changing the NOT IN to simply an IN

    SELECT DISTINCT C.Cust_Num

    FROM Customer C

    WHERE C.Cust_Num IN (SELECT DISTINCT Cust_Num FROM fs_Unit)

    Returns 9K rows.

    So, why does the initial query, which should return approximately 12K rows, return nothing, and how would I fix it?

    Seth Phelabaum


    Consistency is only a virtue if you're not a screwup. 😉

    Links: How to Post Sample Data[/url] :: Running Totals[/url] :: Tally Table[/url] :: Cross Tabs/Pivots[/url] :: String Concatenation[/url]

  • i think if this query:

    SELECT DISTINCT Cust_Num FROM fs_Unit

    returns a null in the distinct collection, you'll get no results;

    you might need

    SELECT DISTINCT Cust_Num FROM fs_Unit WHERE Cust_Num IS NOT NULL

    Lowell


    --help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

  • Garadin (11/7/2008)


    This isn't a problem anymore (it almost was, I was 90% of the way done with a forum post when I figured it out), but due to how weird/interesting it is, I decided to post it anyways.

    I wrote a very simple query:

    SELECT DISTINCT C.Cust_Num

    FROM Customer C

    WHERE C.Cust_Num NOT IN (SELECT DISTINCT Cust_Num FROM fs_Unit)

    I was expecting approximately 12k rows. I got... none.

    The outer query:

    SELECT DISTINCT C.Cust_Num FROM Customer C

    Returns 21K rows.

    The NOT IN query alone:

    SELECT DISTINCT Cust_Num FROM fs_Unit

    Returns 9K rows.

    Changing the NOT IN to simply an IN

    SELECT DISTINCT C.Cust_Num

    FROM Customer C

    WHERE C.Cust_Num IN (SELECT DISTINCT Cust_Num FROM fs_Unit)

    Returns 9K rows.

    So, why does the initial query, which should return approximately 12K rows, return nothing, and how would I fix it?

    What does this return

    SELECT DISTINCT

    C.Cust_Num

    FROM

    Customer C

    left outer join fs_Unit F

    on (C.Cust_Num = F.Cust_Num)

    WHERE

    F.Cust_Num is null

  • @lowell:

    That's what I ended up doing to fix it, but I still don't fully understand why it should be necessary. Could you explain why a single NULL invalidates my entire result set?

    Seth Phelabaum


    Consistency is only a virtue if you're not a screwup. 😉

    Links: How to Post Sample Data[/url] :: Running Totals[/url] :: Tally Table[/url] :: Cross Tabs/Pivots[/url] :: String Concatenation[/url]

  • Lynn,

    That runs fine, and gives the 12K rows expected. There's no reason I couldn't have done it that way, it was just blowing my mind that the above way wasn't working.

    Seth Phelabaum


    Consistency is only a virtue if you're not a screwup. 😉

    Links: How to Post Sample Data[/url] :: Running Totals[/url] :: Tally Table[/url] :: Cross Tabs/Pivots[/url] :: String Concatenation[/url]

  • IN or NOT IN turns into a series of equalities or non-equalities. Which means NULL is a killer, since that will alway return FALSE (since "NULL is neither equal nor not equal to any value")

    ----------------------------------------------------------------------------------
    Your lack of planning does not constitute an emergency on my part...unless you're my manager...or a director and above...or a really loud-spoken end-user..All right - what was my emergency again?

  • Matt Miller (11/7/2008)


    IN or NOT IN turns into a series of equalities or non-equalities. Which means NULL is a killer, since that will alway return FALSE (since "NULL is neither equal nor not equal to any value")

    I should rephrase that - it's killer in the NOT IN scenario. Why? Look at what it turns into.

    WHERE a in (1,2,3,NULL)

    turns into

    WHERE

    a=1 OR

    A=2 OR

    A=3 OR

    A=NULL --unknown

    all it takes is one of the first three to be true and the whole WHERE is true.

    Now, NOT IN looks like:

    WHERE a not in (1,2,3,NULL)

    turns into

    WHERE

    (a<>1) AND

    (A<>2) AND

    (A<>3) AND

    (A<>NULL) --unknown

    Now - ALL of these statements would have to evaluate to true, which can never happen.....

    ----------------------------------------------------------------------------------
    Your lack of planning does not constitute an emergency on my part...unless you're my manager...or a director and above...or a really loud-spoken end-user..All right - what was my emergency again?

  • Matt:

    Thanks, that makes sense. I thought it might be something like that, but that definitely makes it more clear.

    Seth Phelabaum


    Consistency is only a virtue if you're not a screwup. 😉

    Links: How to Post Sample Data[/url] :: Running Totals[/url] :: Tally Table[/url] :: Cross Tabs/Pivots[/url] :: String Concatenation[/url]

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

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