Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 116519
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T03:11:27+00:00 2026-05-11T03:11:27+00:00

I am working with a set of what is essentially Attribute/Value pairs (there’s actually

  • 0

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) ); 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T03:11:27+00:00Added an answer on May 11, 2026 at 3:11 am

    Okay, I think after several tries and edits, this solution finally works:

    SELECT e1.EntityID, t1.TargetID FROM Entities e1   JOIN Entities e2 ON (e1.EntityID = e2.EntityID)   CROSS JOIN Targets t1   LEFT OUTER JOIN Targets t2 ON (t1.TargetID = t2.TargetID     AND e2.AttributeName = t2.AttributeName     AND e2.AttributeValue = t2.AttributeValue) GROUP BY e1.EntityID, t1.TargetID HAVING COUNT(e2.AttributeValue) = COUNT(t2.AttributeValue); 

    Test data:

    INSERT INTO Entities VALUES   -- exact same attributes, should match  (1, 'Foo1', '123'),  (1, 'Bar1', '123'),  -- same attributes but different values, should not match  (2, 'Foo2', '456'),  (2, 'Bar2', '456'),  -- more columns in Entities, should not match  (3, 'Foo3', '789'),  (3, 'Bar3', '789'),  (3, 'Baz3', '789'),  -- fewer columns in Entities, should match  (4, 'Foo4', '012'),  (4, 'Bar4', '012'),  -- same as case 1, should match Target 1  (5, 'Foo1', '123'),  (5, 'Bar1', '123'),  -- one attribute with different value, should not match  (6, 'A', 'one'),  (6, 'B', 'two');  INSERT INTO Targets VALUES   (1, 'Foo1', '123'),  (1, 'Bar1', '123'),  (2, 'Foo2', 'abc'),  (2, 'Bar2', 'abc'),  (3, 'Foo3', '789'),  (3, 'Bar3', '789'),  (4, 'Foo4', '012'),  (4, 'Bar4', '012'),  (4, 'Baz4', '012'),  (6, 'A', 'one'),  (6, 'B', 'twox'); 

    Test results:

    +----------+----------+ | EntityID | TargetID | +----------+----------+ |        1 |        1 |  |        4 |        4 |  |        5 |        1 |  +----------+----------+ 

    To respond to your comment, here is a query with the tables reversed:

    SELECT e1.EntityID, t1.TargetID FROM Targets t1   JOIN Targets t2 ON (t1.TargetID = t2.TargetID)   CROSS JOIN Entities e1   LEFT OUTER JOIN Entities e2 ON (e1.EntityID = e2.EntityID     AND t2.AttributeName = e2.AttributeName     AND t2.AttributeValue = e2.AttributeValue) GROUP BY e1.EntityID, t1.TargetID HAVING COUNT(e2.AttributeValue) = COUNT(t2.AttributeValue); 

    And here’s the output, given the same input data above.

    +----------+----------+ | EntityID | TargetID | +----------+----------+ |        1 |        1 | |        3 |        3 | |        5 |        1 | +----------+----------+ 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 62k
  • Answers 62k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer Try with version 3.0.7a as Łukasz suggested. BeautifulSoup 3.1 was… May 11, 2026 at 10:11 am
  • added an answer First remove the StartupUri property from App.xaml and then use… May 11, 2026 at 10:11 am
  • added an answer I don't think it's possible. You're asking for a regex… May 11, 2026 at 10:11 am

Related Questions

I am working with a set of what is essentially Attribute/Value pairs (there's actually
I am working with a set of data that looks something like the following.
I am working with a set of data that I have converted to a
I am working with a PC based automation software package called Think'n'Do created by
I am working with a large Java web application from a commercial vendor. I've
I am working with a Visual Studio 2005 C++ solution that includes multiple projects
I am working with a hosting provider who has installed mod_python for me. I
I am working with a log of events where there are about 60 different
I am working with a huge list of URL's. Just a quick question I
I am working with a legacy ASP Classic solution which is load balanced (via

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.