weird sql problem.

  • Hi all,

    I´m not sure what I´m doing bad with this query:

    Situation: I need to select all the purchase order lines(poitem_all) that not exists in the table of requisition lines (preqlines).

    select po_num from poitem_all

    results with all the  purchase orders (po_num) for the table poitem_all. Ok. Then for select only that not exists in preqitem table:

    select po_num from poitem_all
    where poitem_all.ponum not in (select po_num from preqitem)

    results with no records! This is weird due to, I have purchase orders that not exists in preqitem table.

    But this query:

    select po_num  from poitem_all
    except
    (select preqitem.po_num from preqitem)

    results with the correct information.

    Anyone knows what is the trick here?

    Thank you in advance.

     

     

     

     

     

     

  • Is it because you are looking up on a different column: ponum instead of po_num 

    select po_num from poitem_all
    where poitem_all.ponum not in (select po_num from preqitem)
  • You also might have a NULL value for po_num in preqitem.  That would prevent the NOT IN from yielding any results, so add a NOT NULL check.  Or, perhaps even better, use NOT EXISTS instead of NOT IN:

    select po_num from dbo.poitem_all 
    where poitem_all.po_num /*ponum?*/ not in (select po_num from dbo.preqitem where po_num is not null)

    --or, more cleanly, write it like this:
    select po_num from dbo.poitem_all po
    where not exists(select 1 from dbo.preqitem pr where pr.po_num = po.po_num)

    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".

  • Also - be careful with referencing columns without using the table alias.

     Select po_num
    From poitem_all
    Where poitem_all.po_num Not In (Select po_num From preqitem)

    In this query - let's assume the column name in preqitem is actually 'ponum' and not po_num.  If that is the case - the query will execute with no errors but will give you results you are not expecting (actually - most or all rows will match).

     Select po_num
    From poitem_all
    Where poitem_all.po_num Not In (Select i.po_num From preqitem i)

    This simple change - using the table alias i - would cause the query to fail if the table preqitem does not have a column named po_num.

    Jeffrey Williams
    “We are all faced with a series of great opportunities brilliantly disguised as impossible situations.”

    ― Charles R. Swindoll

    How to post questions to get better answers faster
    Managing Transaction Logs

  • Hi Jonathan,

    Yes. My mistake. I wrote  ponum instead of po_num.

    But the problem persists.

    Thank you.

  • Hi Scott

    Your solution worked like a charm!

    Thank you.

  • Hi Jeffrey,

    It was my mistake. The column name is po_num and not ponum.

     

    thank you.

  • I think it's worth a second here to go through why a SQL NOT IN with a NULL value in it causes no values to match.

    SQL Rule 1) A WHERE condition must be true to allow a row to be selected.  Not just "not proven false", but absolutely proven true.

    SQL Rule 2) A comparison against a NULL does not yield TRUE or FALSE, only NULL.

    Ok, that said, run these statements:

    SELECT value FROM ( VALUES(1),(2),(3) ) AS data(value) WHERE value NOT IN (5, 6, 7) --all rows

    SELECT value FROM ( VALUES(1),(2),(3) ) AS data(value) WHERE value NOT IN (5, 6, 7, NULL) --no rows!

    Probably easiest is to think of NULL as "unknown".  When SQL hits the NULL in the value list, is 1 not = NULL?  Since NULL is unknown, SQL can't know.  An unknown value could be 1, after all.  So SQL cannot absolutely say that the NOT IN is true.  But if it's not known to be absolutely true, then the WHERE clause has failed, and the row can't be included in the result from the SELECT.

    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".

  • Hi Scott,

    Thank for share this interesting rules. I was not knowing that!

    I came from Oracle SQL, and these NULL things, works different.

    Thank you again.

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

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