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
 Comparing subsets

Author  Topic 

flamblaster
Constraint Violating Yak Guru

384 Posts

Posted - 2009-08-25 : 00:15:38
I'm going to try to keep this as general as possible as it is very confusing to explain.

Is there a general syntax where you can compare the results of the product of 2 "IN" clauses?

I'd like to return results where some of the results from one "IN" clause are in the results of another "IN" clause. But I only want to see results where all the results in subset B are in subset B.

EX:

Select ID, Column From Table Where Column in ('Cat', 'Dog', 'Frog')
Subset A
Results:
ID Column
1 Cat
2 Dog
3 Frog

Select ID, Column From Table Where Column in ('Cat', 'Dog', 'Frog')
Subset B
Results:
ID Column
11 Cat
11 Dog

Desired results:

11 Cat
11 Dog

Important note, if another value ('lion' for example) were present in subset B, I would have wanted to exclude it.

Also, the table that I'm working with is the same in both examples. There is a boolean column that differentiates between the two results though. There is no parent/child or source id.

Thanks!

bklr
Master Smack Fu Yak Hacker

1693 Posts

Posted - 2009-08-25 : 01:19:54
select t.id,t.column
from (Select ID, Column From Table Where Column in ('Cat', 'Dog', 'Frog')) t
inner join (Select ID, Column From Table Where Column in ('Cat', 'Dog', 'Frog'))s on s.column = t.column
Go to Top of Page

flamblaster
Constraint Violating Yak Guru

384 Posts

Posted - 2009-08-25 : 20:10:11
Hi bklr...is there anyway to do this without putting the values in? Also, I don't want to see results where "Cat"occurs once but not "Dog". Both "Cat"and "dog" have to occur in Table
B at least once because they happened once in Table A.
Go to Top of Page
   

- Advertisement -