I would like to grab data that have territory 62 and 69 but that do not have territory 200 and 92 , I am using this query but that doesn't work
select rtp1.releaseId, rtp1.territoryId
from ReleaseTerritoryPrice rtp1
where rtp1.territoryId in (62,69)
and not exists (select releaseId
from ReleaseTerritoryPrice t2
where t2.territoryId in (200,92)
and rtp1.releaseId = t2.releaseId);
Any Help ? Thanks.
This is an example of a set-within-sets query. I like to use aggregation with a having clause for this, because this is a very flexible approach:
Each condition in the
having
clause is counting the number of rows with each of the territories. The first two are saying that62
and69
must be present (the counts are greater than 1). The last two are saying that200
and92
are not present (the counts are 0).As an example, if you wanted to change this so that only one of
62
and69
were needed but not the other two, thehaving
clause would be: