Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Help explaining query results.


Help explaining query results.

Author
Message
Don.
Don.
Valued Member
Valued Member (53 reputation)Valued Member (53 reputation)Valued Member (53 reputation)Valued Member (53 reputation)Valued Member (53 reputation)Valued Member (53 reputation)Valued Member (53 reputation)Valued Member (53 reputation)

Group: General Forum Members
Points: 53 Visits: 271
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
demonfox
demonfox
Ten Centuries
Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)

Group: General Forum Members
Points: 1219 Visits: 1192
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 Ermm
Jason-299789
Jason-299789
Ten Centuries
Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)

Group: General Forum Members
Points: 1134 Visits: 3229
This URL may explain it better http://sqlinthewild.co.za/index.php/2009/08/17/exists-vs-in/

_________________________________________________________________________
SSC Guide to Posting and Best Practices
Luis Cazares
Luis Cazares
SSCrazy Eights
SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)

Group: General Forum Members
Points: 8562 Visits: 18143
Jason-299789 (1/29/2013)
This URL may explain it better http://sqlinthewild.co.za/index.php/2009/08/17/exists-vs-in/


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.
General Disclaimer:
Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?


How to post data/code on a forum to get the best help: Option 1 / Option 2
Adi Cohn-120898
Adi Cohn-120898
SSCrazy
SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)

Group: General Forum Members
Points: 2224 Visits: 6079
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/
Don.
Don.
Valued Member
Valued Member (53 reputation)Valued Member (53 reputation)Valued Member (53 reputation)Valued Member (53 reputation)Valued Member (53 reputation)Valued Member (53 reputation)Valued Member (53 reputation)Valued Member (53 reputation)

Group: General Forum Members
Points: 53 Visits: 271
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)


Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search