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 8642441
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T11:46:36+00:00 2026-06-12T11:46:36+00:00

I have two tables like so: create table doi ( id number primary key

  • 0

I have two tables like so:

create table doi (
    id number primary key not null,
    -- Other columns omitted
);

create table doi_membership (
    id number primary key not null,
    doi_id number not null, --foreign key to doi.id
    key_1 number not null,
    key_2 number not null,
    -- Other columns omitted
);

I have a set of key_1, key_2 pairs and I want to see if there is a doi that contains the complete set of the given key_1, key_2 pairs and nothing more. I can’t seem to think of the SQL necessary to do this. I am using an oracle database if that helps. Any ideas?

Update:

I don’t think I explained this very well so I will give an example.

Conceptually, a single DOI contains a list of key_1, key_2 pairs. I have my own list of key_1, key_2 pairs and I want to see if there exists a DOI where its list of pairs exactly matches my list of pairs.

So, lets say there is a DOI that has the following list of key_1, key_2 pairs (each of these rows is a separate doi_membership row related to the same doi):

1, 2
3, 4
5, 6

And another DOI with the following list of pairs:

1, 2
3, 4
5, 6
7, 8

The set of pairs that I have are

1, 2
3, 4
5, 6

I want to match the first DOI given because its set of pairs and my set of pairs match exactly. The second DOI should not be matched.

I hope that clears things up.

  • 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. Editorial Team
    Editorial Team
    2026-06-12T11:46:38+00:00Added an answer on June 12, 2026 at 11:46 am

    I think the best way to approach set membership is using the HAVING clause in SQL. The idea is to group the elements of the set together, in this case, the membership records for each doi, and then do tests at the individual level.

    For instance, the following having clause would check that keyval1 is present:

    having sum(case when keyval_1 = <keyval1> then 1 else 0 end) > 0
    

    It does this by summing the number of records with keyval_1 = <keval1>. If there is more than 0, then “doi_id” meets the condition.

    Your conditions are a bit mroe complicated because you are looking at pairs of values. One way to solve this is by concatenating the values together (not necessary, but it somewhat simplifies the logic). The following clause verifies that only your value pairs are present on a doi_id:

    having sum(case when concat(key_1, ',', key_2) in (<key value pairs here>)
                    then 0 else 1 end) = 0
    

    What it does is count the number of records where the key pairs don’t match. If there are any, the comparison fails. You need to concateante your key value pairs for the in clause. Something like in ('1,1', '2,2', '3,14').

    To generalize to the condition that all match, I use the following:

    select doi_id
    from doi_membership
    group by doi_id
    having sum(case when concat(key_1, ',', key_2) in (<key value pairs here>)
                    then 0 else 1 end) = 0 and 
           sum(case when concat(key_1, ',', key_2) = <key pair 1>
                    then 1 else 0 end) > 0 and
           sum(case when concat(key_1, ',', key_2) = <key pair 2>
                    then 1 else 0 end) > 0 and
           . . .
           sum(case when concat(key_1, ',', key_2) = <key pair n>
                    then 1 else 0 end)
    

    The HAVING clause first tests that all the pairs are present. The remaining clauses then test for the presence of each pair.

    There are other approaches. I find that the HAVING clause is the most general, because it can accommodate a wide range of logic on the inclusion criteria for the sets.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So I have two tables structured like so: CREATE TABLE #nodes(node int NOT NULL);
I have two tables like, CREATE TABLE [dbo].[entry] ( [id] [int] NULL, [service] [int]
I have the following two tables CREATE TABLE accounts ( id INTEGER UNSIGNED NOT
I have two tables which looks something like this Table Queue int ID; string
So here's the setup. I have two tables: CREATE TABLE dbo.TmpFeesToRules1(Name varchar, LookupId int)
here are my two tables of concern: CREATE TABLE IF NOT EXISTS `tutorial` (
I have two tables, one with monster names and their respective scores: CREATE TABLE
I have two tables: Table_A name1 create_date Table_B name2 keep_time name1 is something like
I have two tables like so; id_image foo bar 1 3 5 2 8
I have two tables like the ones below. I need to find what exchangeRate

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.