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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T16:03:55+00:00 2026-05-26T16:03:55+00:00

Each ‘Product’ can have as many as 10000 ‘Segment’ rows. The segments have a

  • 0

Each ‘Product’ can have as many as 10000 ‘Segment’ rows. The segments have a sort column that starts at 1 for each product (1, 2, 3, 4, 5, …) and a value column that can contain any values such as (323.113, 5423.231, 873.42, 422.64, 763.1, …).

I would like to identify potential matches for products given a subset of segments. For example, if I have 5 segment values in the correct order, how can I efficiently find all the products which have all 5 segments in the same order somewhere in the Segment table?


Update

I posted a follow-up question to this here: Find a series of data using non-exact measurements (fuzzy logic)

  • 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-26T16:03:56+00:00Added an answer on May 26, 2026 at 4:03 pm

    Assume tables like so:

    CREATE TABLE Products
     (
       ProductId  int           not null
        constraint PK_Products
         primary key
      ,Name       varchar(100)  not null
     )
    
    CREATE TABLE Segments
     (
       ProductId   int    not null
        constraint FK_Segments__Products
         foreign key references Products (ProductId)
      ,OrderBy     int    not null
      ,Value       float  not null
      ,constraint PK_Segments
        primary key (ProductId, OrderBy)
     )
    

    Next, set up your search data in a temp table:

    CREATE TABLE #MatchThis
     (
       Position  int    not null
      ,Value     float  not null
     )
    

    For N search objects, this has to be populated like so

    First item   0    <value 1>
    Second item  1    <value 2>
    Third item   2    <value 3>
    ...
    Nth item     N-1  <value N>
    

    Now set up some important values. (This could be crammed into the final query, but this way makes it easier to read, and may improve performance slightly.)

    DECLARE
      @ItemCount   int
     ,@FirstValue  float
    
    --  How many items to be matched ("N", above)
    SELECT @ItemCount = count(*)
     from #MatchThis
    
    --  The value of the first item in the search set
    SELECT @FirstValue = Value
     from #MatchThis
     where Position = 0
    

    And then it’s just a query:

    SELECT
       pr.Name
      ,fv.OrderBy  --  Required by the Group By, but otherwise can be ignored
     from #MatchThis mt
      cross join (--  All Segments that match the first value in the set
                  select ProductId, OrderBy
                   from Segment
                   where Value = @FirstValue) fv
      inner join Product pr  --  Just to get the Product name
       on pr.ProductId = fv.ProductId
      inner join Segment se
       on se.ProductId = fv.ProductId
        and se.OrderBy = fv.OrderBy + mt.Position  --  Lines them up based on the first value
        and se.Value = mt.Value                    --  No join if the values don't match
     group by
       pr.Name
      ,fv.OrderBy
     having count(*) = @ItemCount  --  Only include if as many segments pulled for this product/segment.OrderBy as are required
    

    I am convinced this will work, but I don’t have time to test it out in detail just now. To optimize for performance, besides the primary keys indicated you could add a regular index on Segment.Value

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

Sidebar

Related Questions

Each of my clients can have many todo items and every todo item has
Each product in my database can have at least three and sometimes four or
Each of my user's have a set number of slots for storage. They can
Each user of our application can have different format for Date and Time. I
Each time a python file is imported that contains a large quantity of static
Each item in a WPF ListBox control seems to have a bit of left
Each new problem that I think I will be able to solve, it turns
Each key in a hash has a value that's also a hash. { 100
Each camera folder has many thousands of photos, I need to open 20-or so
Each user HAS MANY photos and HAS MANY comments. I would like to order

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.