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
 topic 1

Author  Topic 

deeptish
Starting Member

1 Post

Posted - 2012-06-05 : 07:47:39
Can somebody help me by writing a query that retrieve the unique combination of records (value1,value2) those are present in query 1 but not in query 2.

NOTE : table 2 is much bigger than table 1 and both the table may have duplicate values

Query 1

Select value1,value2 from <table 1>
group by value1,value2
order by value1,value2

Query 2

Select value1,value2 from <table 2>
group by value1,value2
order by value1,value2


Thanks
Deeptish

nigelrivett
Master Smack Fu Yak Hacker

3385 Posts

Posted - 2012-06-05 : 09:32:11
Select distinct t1.value1,t1.value2
from [table 1] t1
left join [table 2] t2
on t1.value1 = t2.value1
and t1.value2 = t2.value2
where t2.value1 is null




==========================================
Cursors are useful if you don't know sql.
SSIS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-06-05 : 10:24:33
[code]
Select value1,value2 from <table 1> t1
WHERE NOT EXISTS (SELECT 1 FROM <table 2>
WHERE value1 = t1.value1
AND value2 = t1.value2
)
[/code]

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -