I have a table as follows
OWNERID
PET_QUANTITY
PET_TYPE
PET_TYPE is either DOG, CAT, or BIRD
There is one record per owner/pet so if have a dog and bird there would be two records and pet_quantity would be 2 in both records.
I want all the owner_IDs where the person has two pets and the pets are a dog and a cat.
First off, the data model is rather unfortunate since the value for
PET_QUANTITYfor a particular row depends on data in other rows. That violates basic normalization rules. And that, in turn, creates a situation where you will inevitably get out of sync and so you have to determine whether thePET_QUANTITYrules or whether the number of rows in the table rules. You’d be much better served by a properly normalized data model.Assuming that
PET_QUANTITYrules when there is a discrepancy, one option would be to use anINTERSECTAnother would be to do something like