简体   繁体   中英

SQL Subquery Returning Strange Results

I'm writing a very simple subquery in MS SQL that's returning unexpected results. What I'm trying to return is a list of acct numbers that exists in one time period but not another time period. My initial query (pasted below) returned zero rows which is unexpected.

select
    name1.acct
from
    sym.dbo.name as name1
where
    name1.processdate = 20171130
    and name1.type = 0
    and name1.acct not in
    (
    select 
        name2.acct
    from 
        sym.dbo.name as name2
    where
        name2.type = 0
        and name2.processdate = 20171031
    )

However, when I add one extra line of logic to the subquery and execute the expected results are returned to me, see below.

select
    name1.acct
from
    sym.dbo.name as name1
where
    name1.processdate = 20171130
    and name1.type = 0
    and name1.acct not in
    (
    select 
        name2.acct
    from 
        sym.dbo.name as name2
    where
        name2.type = 0
        and name2.processdate = 20171031
        and name2.acct <> '8888888'
    )

Initially I thought it may be related to data type so I tried converting the acct number to varchar and int with the same results (using convert as opposed to cast). Can someone explain to me why query one returned zero results and query two returned the expected results?

I'm using Microsoft SQL Management Studio 2016.

Thanks

Don't use NOT IN , especially with subqueries. It does not behave as expected -- as you just learned. The problem? If any value returned by the subquery is NULL , then NOT IN filters everything out.

Instead, use NOT EXISTS :

where name1.processdate = 20171130 and
      name1.type = 0 and
      not exists (select 1
                  from sym.dbo.name name2
                  where name2.acct = name1.acct and
                        name2.processdate = 20171031
                 );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM