Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a simple table with two columns, Column A and B

A, B
1, 1
1, 2
1, 3
1, 4
1, 5

2, 1
2, 2
2, 3

3, 2
3, 6
3, 4

4, 1
4, 2
4, 3

Now I only want Values of Column A where Column B MUST have the values of 1,2 and 3
So the result would be 2 and 4.

I simply cant remember my SQL today... any help ??

SELECT DISTINCT(A) FROM my_Table WHERE ??????
Posted
Comments
CHill60 1-May-13 8:01am    
I was going to post <pre lang="sql">where B IN (1,2,3)</pre> but that would give you the result 1,2,3,4 ... so do you mean A must only be reported if B has all of 1 and 2 and 3 ? But from your sample data that would return 1, 2 and 4 ... can you clarify
Paw Jershauge 1-May-13 8:04am    
Hey Chill60
Yes A should only be shown, if B contains 1,2,3 nothing more nothing less.
So 1 from A would not be shown as it also contains 4 and 5 in B column
CHill60 1-May-13 8:06am    
gotcha. I'll have another look at it

1 solution

Probably not the nicest way to do this but it works and does have the advantage that it will work for any version of SQL
SQL
select DISTINCT (A) from my_Table
where A in (select A from my_Table where B = 1)
and A in (select A from my_Table where B = 2)
and A in (select A from my_Table where B = 3)
and A not in (select A from my_Table where B not in (1,2,3))

[Edit - made the last criterion a bit more generic]
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900