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

  • SEARCH
  • Home
  • 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 798963
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T23:03:06+00:00 2026-05-14T23:03:06+00:00

I have a table whose records represent certain objects. For the sake of simplicity

  • 0

I have a table whose records represent certain objects. For the sake of simplicity I am going to assume that the table only has one column, and that is the unique ObjectId. Now I need a way to store combinations of objects from that table. The combinations have to be unique, but can be of arbitrary length. For example, if I have the ObjectIds

1,2,3,4

I want to store the following combinations:

{1,2}, {1,3,4}, {2,4}, {1,2,3,4}

The ordering is not necessary. My current implementation is to have a table Combinations that maps ObjectIds to CombinationIds. So every combination receives a unique Id:

ObjectId | CombinationId
------------------------
1        | 1
2        | 1
1        | 2
3        | 2
4        | 2

This is the mapping for the first two combinations of the example above. The problem is, that the query for finding the CombinationId of a specific Combination seems to be very complex. The two main usage scenarios for this table will be to iterate over all combinations, and the retrieve a specific combination. The table will be created once and never be updated. I am using SQLite through JDBC. Is there any simpler way or a best practice to implement such a mapping?

  • 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-05-14T23:03:07+00:00Added an answer on May 14, 2026 at 11:03 pm

    The problem is, that the query for finding the CombinationId of a specific Combination seems to be very complex.

    Shouldn’t be too bad. If you want all combinations containing the selected items (with additional items allowed), it’s just something like:

    SELECT combinationID
    FROM Combination
    WHERE objectId IN (1, 3, 4)
    GROUP BY combinationID
    HAVING COUNT(*) = 3 -- The number of items in the combination
    

    If you need only the specific combination (no extra items allowed), it can be more like:

    SELECT combinationID FROM (
       -- ... query from above goes here, this gives us all with those 3
    ) AS candidates
    
    -- This bit gives us a row for each item in the candidates, including 
    -- the items we know about but also any 'extras'
    INNER JOIN combination ON (candidates.combinationID = combination.combinationID)
    
    GROUP BY candidates.combinationID
    HAVING COUNT(*) = 3 -- Because we joined back on ALL, ones with extras will have > 3
    

    You can also use a NOT EXISTS here (or in the original query), this seemed easier to explain.

    Finally you could also be fancy and have a single, simple query

    SELECT combinationID
    FROM Combination AS candidates
    INNER JOIN Combination AS allItems ON 
      (candidates.combinationID = allItems.combinationID)
    WHERE candidates.objectId IN (1, 3, 4)
    GROUP BY combinationID
    HAVING COUNT(*) = 9 -- The number of items in the combination, squared
    

    So in other words, if we’re looking for {1, 2}, and there’s a combination with {1, 2, 3}, we’ll have a {candidates, allItems} JOIN result of:

    {1, 1}, {1, 2}, {1, 3}, {2, 1}, {2, 2}, {2, 3}
    

    The extra 3 results in COUNT(*) being 6 rows after GROUPing, not 4, so we know that’s not the combination we’re after.

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

Sidebar

Ask A Question

Stats

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

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

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

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

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You are searching for a solution to the Diamond Problem.… May 14, 2026 at 11:09 pm
  • Editorial Team
    Editorial Team added an answer Well the Visual Studio SDK would be your path to… May 14, 2026 at 11:09 pm
  • Editorial Team
    Editorial Team added an answer More than likely it's your privacy settings. The example on… May 14, 2026 at 11:09 pm

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.