Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 problem to compare data of two tables

Author  Topic 

eugz
Posting Yak Master

210 Posts

Posted - 2014-12-04 : 15:40:50
Hi All.

I try to compare and select data from table that which not in other table

Select
AAA_Id
,BBB
From dbo.Table1 a
Where AAA_Id not in(Select b.AAA_Id
From dbo.Table2 b, dbo.Table1 a
Where b.AAA_Id = 11)

but when I modified it like:
Select 
AAA_Id
,BBB
From dbo.Table1 a
union
Select
0
,'Select'
From dbo.Table1 a
Where AAA_Id not in(Select b.AAA_Id
From dbo.Table2 b, dbo.Table1 a
Where b.AAA_Id = 11)
Order by 2

I cannot to get same result. How to fix problem?

Thanks.

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2014-12-04 : 15:57:57
I don't understand your code modification. You are missing an ORDER BY, so it could be in a different order than your first query.

Tara Kizer
SQL Server MVP since 2007
http://weblogs.sqlteam.com/tarad/
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2014-12-04 : 16:31:28
You edited your post to add an ORDER BY. I didn't want anyone to think I'm crazy with my last reply.

Tara Kizer
SQL Server MVP since 2007
http://weblogs.sqlteam.com/tarad/
Go to Top of Page
   

- Advertisement -