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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T03:23:37+00:00 2026-05-31T03:23:37+00:00

I have a table that contains URL strings, i.e. /A/B/C /C/E /C/B/A/R Each string

  • 0

I have a table that contains URL strings, i.e.

/A/B/C
/C/E
/C/B/A/R

Each string is split into tokens where the separator in my case is ‘/’. Then I assign integer value to each token and the put them into dictionary (different database table) i.e.

A : 1
B : 2
C : 3
E : 4
D : 5
G : 6
R : 7

My problem is to find those rows in first tables which contain given sequence of tokens. Additional problem is that my input is sequence of ints, i.e. I have

3, 2

and I’d like to find following rows

/A/B/C
/C/B/A/R

How to do this in efficient way. By this I mean how to design proper database structure.

I use PostgreSQL, solution should work well for 2 mln of rows in first table.

To clarify my example – I need both ‘B’ AND ‘C’ to be in the URL. Also ‘B’ and ‘C’ can occur in any order in the URL.

I need efficient SELECT. INSERT does not have to be efficient. I do not have to do all work in SQL if this changes anything.

Thanks in advance

  • 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-31T03:23:38+00:00Added an answer on May 31, 2026 at 3:23 am

    I’m not sure how to do this, but I’m just giving you some idea that might be useful. You already have your initial table. You process is and create the token table:

    +------------+---------+
    | TokenValue | TokenId |
    +------------+---------+
    | A          |       1 |
    | B          |       2 |
    | C          |       3 |
    | E          |       4 |
    | D          |       5 |
    | G          |       6 |
    | R          |       7 |
    +------------+---------+
    

    That’s ok for me. Now, what I would do is to create a new table in which I would match the original table with the tokens of the token table (OrderedTokens). Something like:

    +-------+---------+---------+
    | UrlID | TokenId | AnOrder |
    +-------+---------+---------+
    |     1 |       1 |       1 |
    |     1 |       2 |       2 |
    |     1 |       3 |       3 |
    |     2 |       5 |       1 |
    |     2 |       2 |       2 |
    |     2 |       1 |       3 |
    |     2 |       7 |       4 |
    |     3 |       3 |       1 |
    |     3 |       4 |       2 |
    +-------+---------+---------+
    

    This way you can even recreate your original table as long as you use the order field. For example:

    select string_agg(t.tokenValue, '/' order by ot.anOrder) as OriginalUrl
    from OrderedTokens as ot
    join tokens t on t.tokenId = ot.tokenId
    group by ot.urlId
    

    The previous query would result in:

    +-------------+
    | OriginalUrl |
    +-------------+
    | A/B/C       |
    | D/B/A/R     |
    | C/E         |
    +-------------+
    

    So, you don’t even need your original table anymore. If you want to get Urls that have any of the provided token ids (in this case B OR C), you sould use this:

    select string_agg(t.tokenValue, '/' order by ot.anOrder) as OriginalUrl
    from OrderedTokens as ot
    join Tokens t on t.tokenId = ot.tokenId
    group by urlid
    having count(case when ot.tokenId in (2, 3) then 1 end) > 0
    

    This results in:

    +-------------+
    | OriginalUrl |
    +-------------+
    | A/B/C       | => It has both B and C
    | D/B/A/R     | => It has only B
    | C/E         | => It has only C
    +-------------+
    

    Now, if you want to get all Urls that have BOTH ids, then try this:

    select string_agg(t.tokenValue, '/' order by ot.anOrder) as OriginalUrl
    from OrderedTokens as ot
    join Tokens t on t.tokenId = ot.tokenId
    group by urlid
    having count(distinct case when ot.tokenId in (2, 3) then ot.tokenId end) = 2
    

    Add in the count all the ids you want to filter and then equal that count the the amount of ids you added. The previous query will result in:

    +-------------+
    | OriginalUrl |
    +-------------+
    | A/B/C       | => It has both B and C
    +-------------+
    

    The funny thing is that none of the solutions I provided results in your expected result. So, have I misunderstood your requirements or is the expected result you provided wrong?

    Let me know if this is correct.

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

Sidebar

Related Questions

I have a table that contains thousands of URLs. Each URL will have a
I have a database that contains a table for storing URL Images (since storing
I have a table that contains tasks and I want to give these an
I have a table that contains maybe 10k to 100k rows and I need
I have a table that contains some blob fields that I don't want to
I have a table that contains intervals: CREATE TABLE tbl ( user_id: INTEGER, start:
I have a table that contains id, created, user_id. I'm wondering if there is
I have a table that contains members names and a field with multiple ID
I have a table that contains log entries for a program I'm writing. I'm
Here is my situation: I have one table that contains a list of drugs

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.