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
 need help on retriving data not in another table

Author  Topic 

Y2K

2 Posts

Posted - 2009-06-16 : 04:49:13
I have two tables- T1 and T2. The table T2 is a subset of table T1. And i want to retrive the uncommon data found in T1.But one more problem here is, If i query T1, i get 5 results in 5 rows and when i query T2 i get result in 1 row where values are seperated by comma. This is where i am finding problem.

For example: Once I query T1, i get result like this,
a
b
c
d
e

and when I query T2 it results like below,
a,b,c

Note here that from two tables: d,e are uncomman values(got from T1) and a,b,c (present in both tables)are common values and also the T2 query results in single row.
Now here, I am intrested in retriving these uncomman values(i.e., d,e)from T1 leaving the common values
Can someone help me on this..

Thanks.

raky
Aged Yak Warrior

767 Posts

Posted - 2009-06-16 : 05:33:37
try like this


declare @table1 table ( id1 int, val1 varchar(8))
insert into @table1
select 1, 'a' union all
select 2, 'b' union all
select 3, 'c' union all
select 4, 'd' union all
select 5, 'e'

declare @table2 table ( id2 int, val2 varchar(8))
insert into @table2
select 1, 'a' union all
select 2, 'b' union all
select 3, 'c'

solution1 : select t1.val1
from @table1 t1
left join @table2 t2 on t2.val2 = t1.val1
where t2.val2 is null

solution2 : select t1.val1
from @table1 t1
where not exists ( select val2 from @table2 where val2 = t1.val1 )


Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2009-06-16 : 05:39:40
See http://weblogs.sqlteam.com/peterl/archive/2009/06/12/Timings-of-different-techniques-for-finding-missing-records.aspx


E 12°55'05.63"
N 56°04'39.26"
Go to Top of Page
   

- Advertisement -