Given a parent + reference tables where Reference table is as follows
Ref_ID PARENT_ID
-------------------
1 1
2 1
1 2
3 2
1 3
3 3
4 3
2 4
3 4
I’m trying to return all distinct parent rows where ref_id contains both 2 & 3
The query
SELECT *
FROM Parent
WHERE parent_id in (SELECT parent_id from XRefTable where ref_id in (2, 3) )
returns all parent_id 1, 2, 3, 4
WHEREAS the correct result required is to return parent_id 4 which has BOTH ref_id’s 2 & 3, others have EITHER 2 OR 3
Any help is appreciated
FYI – there are 4-7 tables in the query (depending on user selections) so performance is a huge factor
SORRY cannot use stored procedures as it has to work on SQL Server CE too
1 Answer