|
|
|
SSC Rookie
      
Group: General Forum Members
Last Login: Yesterday @ 5:00 AM
Points: 42,
Visits: 206
|
|
Hi Guys,
I was working on a script that would show all records in Table A that didnt have corresponding records in Table B.
I thought it would be simple enough and wrote the script below, which returned 0 rows. There should definitely be a couple thousand rows returned and am fairly concerned \ curious as to why the script below is incorrect.
Select * from Contact1 Where Company <> 'Z_CANDIDATE' And Accountno not in (Select Distinct Client_Accountno from RSM_KPI) I amended the script to the script below, and its returned the correct results.
Select * from Contact1 Where Company <> 'Z_CANDIDATE' AND NOT EXISTS (Select Client_Accountno from RSM_KPI Where RSM_KPI.Client_Accountno = Contact1.Accountno)
Could someone explain \ point me to some links that would explain the discrepancy?
Thanks
Don
|
|
|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Today @ 4:10 AM
Points: 1,092,
Visits: 1,122
|
|
Don. (1/29/2013)
Hi Guys, I was working on a script that would show all records in Table A that didnt have corresponding records in Table B. I thought it would be simple enough and wrote the script below, which returned 0 rows. There should definitely be a couple thousand rows returned and am fairly concerned \ curious as to why the script below is incorrect. Select * from Contact1 Where Company <> 'Z_CANDIDATE' And Accountno not in (Select Distinct Client_Accountno from RSM_KPI) I amended the script to the script below, and its returned the correct results. Select * from Contact1 Where Company <> 'Z_CANDIDATE' AND NOT EXISTS (Select Client_Accountno from RSM_KPI Where RSM_KPI.Client_Accountno = Contact1.Accountno)
Could someone explain \ point me to some links that would explain the discrepancy? Thanks Don Second Query does row by row check for the Contact table record in RSM_KPI table record ; First query , just check if any record of account_no in contacts table is not present in RSM_KPI table ; I guess it should return true for both Exists and Not Exists..
~ demonfox ___________________________________________________________________ Wondering what I would do next , when I am done with this one
|
|
|
|
|
SSC Eights!
      
Group: General Forum Members
Last Login: Today @ 10:57 AM
Points: 838,
Visits: 2,200
|
|
|
|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Today @ 4:16 PM
Points: 1,091,
Visits: 2,205
|
|
I believe that this article explains better this situation http://sqlinthewild.co.za/index.php/2010/02/18/not-exists-vs-not-in/
The article is really interesting and worth reading but the conclusion is this: "Most importantly, NOT EXISTS and NOT IN do not have the same behaviour when there are NULLs involved."
Your task is to read it to understand why.
Luis C. Please don't trust me, test the solutions I give you before using them. Forum Etiquette: How to post data/code on a forum to get the best help
|
|
|
|
|
SSCrazy
      
Group: General Forum Members
Last Login: Yesterday @ 10:47 PM
Points: 2,023,
Visits: 4,948
|
|
Do you have the value NULL in column client_accountno? It seems that you have it. You have to take into account NULLs. Check the code bellow that shows it:
--Creating the tables create table Demo (I INT NULL) go create table Demo2 (I INT NULL) go
--Inserting the data insert into Demo (I) select 1 union select 2 go
insert into Demo2 (I) select 1 union select 3 go
--At this point Demo2 does not --contain the value NULL, so this should work select * from Demo where I not in (select I from Demo2)
select * from Demo where not exists (select Demo2.I from Demo2 where Demo2.I = Demo.I)
--After inserting the value Null the first query --won't return any records insert Demo2 (I) values (NULL)
select * from Demo where I not in (select I from Demo2)
--I need to exclude the value Null from the subquery select * from Demo where not exists (select Demo2.I from Demo2 where Demo2.I = Demo.I)
--cleanup drop table Demo drop table Demo2
Adi
-------------------------------------------------------------- To know how to ask questions and increase the chances of getting asnwers: http://www.sqlservercentral.com/articles/Best+Practices/61537/
For better answers on performance questions, click on the following... http://www.sqlservercentral.com/articles/SQLServerCentral/66909/
|
|
|
|
|
SSC Rookie
      
Group: General Forum Members
Last Login: Yesterday @ 5:00 AM
Points: 42,
Visits: 206
|
|
Hi Guys,
Thanks for the replies.
Adding the where clause below returned the correct results.
WHERE Client_Accountno is not null
Select * from Contact1 Where Company <> 'Z_CANDIDATE' And Accountno not in (Select Distinct Client_Accountno from RSM_KPI WHERE Client_Accountno is not null)
Its still going to take some getting used to the script below not working though...
Select * from Contact1 Where Company <> 'Z_CANDIDATE' And Accountno not in (Select Distinct Client_Accountno from RSM_KPI)
|
|
|
|