My problem domain is advertising, and, to that end, I have a database which contains a table called ADVERT. An advert can have facets (i.e. quasi-taxonomic descriptive terms). So, there’s a FACET table which defines the facets and a FACETTERM table which contains the values for each facet. ADVERTFACETTERMASSIGNMENT is the link table which says which facet term values are allocated to which advert.
So, you may have an advert for a car which has a value “Honda” for it’s “Make” facet, and “Sussex” for its “Location” facet. So if the advert is advert {PK = 14}, and Honda is facet-term {PK = 1} and Sussex is facet-term {PK = 2}, you expect to rows in ADVERTFACETTERMASSIGNMENT { AdvertId, FacetTermId }: 14, 1 and 14, 2.
Given this arrangement, how do I go about finding all the other adverts which are for Hondas in Sussex? In other words, how do I find a set of rows in ADVERTFACETTERMASSIGNMENT which match the rows from that table for a given advert but which are not from that advert?
I am using SQL Server 2008. I have tried using an IN clause, but this returns partial matches, i.e. all Hondas other than in Sussex and all cars in Sussex which aren’t Hondas etc.
To restate my requirements, I need to find all rows from ADVERTFACETTERMASSIGNMENT where those rows contain at least the same facet-term ids as those for another given advert. It doesn’t matter if it has more facet-terms, provided it has at least exactly the same facet-terms as the chosen, comparator, advert.
This is basically an EAV – Entity, Attribute, Value model with fixed selections of values.
The technique here is that the number of matched facets in the inner join between any two adverts has to be equal to the number of facets of the object advert on the left hand side.