I am working with a set of what is essentially Attribute/Value pairs (there’s actually quite a bit more to this, but I’m simplifying for the sake of this question). Effectively you can think of the tables as such:
Entities (EntityID,AttributeName,AttributeValue) PK=EntityID,AttributeName
Targets (TargetID,AttributeName,AttributeValue) PK=TargetID,AttributeName
How would you query with SQL the set of EntityID,TargetID for which an Entity has all the attributes for a target as well as the corresponding value?
EDIT (DDL as requested):
CREATE TABLE Entities( EntityID INTEGER NOT NULL, AttributeName CHAR(50) NOT NULL, AttributeValue CHAR(50) NOT NULL, CONSTRAINT EntitiesPK PRIMARY KEY (EntityID,AttributeName) ); CREATE TABLE Targets( TargetID INTEGER NOT NULL, AttributeName CHAR(50) NOT NULL, AttributeValue CHAR(50) NOT NULL, CONSTRAINT TargetsPK PRIMARY KEY (TargetID,AttributeName) );
Okay, I think after several tries and edits, this solution finally works:
Test data:
Test results:
To respond to your comment, here is a query with the tables reversed:
And here’s the output, given the same input data above.