Two almost identical queries returning different results

I get different results for the following two queries, and I have no idea why. The only difference is that one has IN and one has equal.

Before I get into queries, you should know that I found a better way to do this by moving the subquery to a common table expression, but it still drives me crazy! I really want to know what caused the problem in the first place, I ask out of curiosity

Here is the first request:

use [DB.90_39733]
Select distinct x.uniqproducer, cn.Firstname,cn.lastname,e.code,
ecn.FirstName, ecn.LastName, ecn.entid, x.uniqline 
from product x
join employ e on e.EmpID=x.uniqproducer
join contactname cn on cn.uniqentity=e.uniqentity 
join [ETL_GAWR92]..idlookupentity ide on ide.enttype='EM' 
                                                        and ide.UniqEntity=e.UniqEntity
left join [ETL_GAWR92]..EntConName ecn on ecn.entid=ide.empid 
                                      and ecn.opt='Y'
Where x.UniqProducer =(SELECT TOP 1 idl.UniqEntity
                               FROM [ETL_GAWR92]..IDLookupEntity idl
                               LEFT JOIN [ETL_GAWR92]..Employ e2 ON e2.ProdID = ''                 
                               WHERE idl.empID = e2.EmpID   AND
                                     idl.EntType     = 'EM')

And the second one:

use [DB.90_39733]
    Select distinct x.uniqproducer, cn.Firstname,cn.lastname,e.code,
    ecn.FirstName, ecn.LastName, ecn.entid, x.uniqline 
    from product x
    join employ e on e.EmpID=x.uniqproducer
    join contactname cn on cn.uniqentity=e.uniqentity 
    join [ETL_GAWR92]..idlookupentity ide on ide.enttype='EM' 
                                                            and ide.UniqEntity=e.UniqEntity
    left join [ETL_GAWR92]..EntConName ecn on ecn.entid=ide.empid 
                                          and ecn.opt='Y'
    Where x.UniqProducer IN (SELECT TOP 1 idl.UniqEntity
                                   FROM [ETL_GAWR92]..IDLookupEntity idl
                                   LEFT JOIN [ETL_GAWR92]..Employ e2 ON e2.ProdID = ''                 
                                   WHERE idl.empID = e2.EmpID   AND
                                         idl.EntType     = 'EM')

The first query returns 0 rows, and the second query returns 2 rows. The only difference is x.UniqProducer = versus x.UniqProducer IN for the last where clause.

thank you for your time

+4
2

SELECT TOP 1 , . ORDER BY , , .

(SELECT TOP 1 idl.UniqEntity
                               FROM [ETL_GAWR92]..IDLookupEntity idl
                               LEFT JOIN [ETL_GAWR92]..Employ e2 ON e2.ProdID = ''                 
                               WHERE idl.empID = e2.EmpID   AND
                                     idl.EntType     = 'EM' ORDER BY idl.UniqEntity)
+4

( "" ), , equals in . equals SQL , , in SQL , , "" "" . , , 1 , , , @RickS, - , "" - () in - , equals.

: ? , , ?

0

Source: https://habr.com/ru/post/1673026/


All Articles