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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T22:09:38+00:00 2026-05-12T22:09:38+00:00

Here’s my problem: a user searches for products by size. The result should show

  • 0

Here’s my problem: a user searches for products by size. The result should show all products of the desired size (if any) plus products progressively larger and smaller until there are at least 50 undersized and 50 oversized products displayed in addition to the correctly-sized products.

The result should always show all products of a certain size; in other words, if moving to the next size up or down will result in more than 50 products, show them all – don’t stop at 50.

Example: Imagine there are 25 distinct sizes with 20 products of each size. The user asks for size 12. We need to go three sizes down and three sizes up to get at least 50 in each direction. The query should return all size-12 products, plus the size 9, 10, 11, 13, 14, and 15 products. The query would return 140 products total (the 20 size-12 plus 60 above and 60 below.)
Unfortunately the sizes are not nice integers like my example. They are arbitrary decimal values.

A Linq to SQL query to do this would be really cool, but plain SQL or C# is welcome, too.
(My environment is C#, SQL Server 2005)
Thanks

  • 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-12T22:09:38+00:00Added an answer on May 12, 2026 at 10:09 pm

    Here’s a sample SQL statement (for mysql) that should do what you want. But depending on what else your procedure is doing, you may find it faster to do some of the processing in the C# code:

    SELECT 
        *
    FROM
        products
    WHERE 
        size = [[desired_size]] OR 
        size IN (
             SELECT DISTINCT 
                 size 
             FROM 
                 products
             WHERE
                 size > [[desired_size]]
             ORDER BY 
                 size
             LIMIT 50
        )
        OR
        size IN (
             SELECT DISTINCT 
                 size 
             FROM 
                 products
             WHERE
                 size < [[desired_size]]
             ORDER BY 
                 size DESC
             LIMIT 50
        )
    

    I’ll explain by starting at the beginning (and use your example for the values)…

    Firstly we need to generate a list of the next 50 larger (or smaller) items. The following query should do this:

    SELECT * FROM products WHERE size > 12 ORDER BY size LIMIT 50
    

    So, right now, we’re grabbing everything from the products table that’s larger than the desired size. We order it by size, then limit it to only the first 50.
    So, in this case, it should return (in this order) 20 products of size 13, 20 products of size 14 and 10 products of size 15.
    You can try this in the Visual Studio SQL editor, and see which rows it returns.

    But for our purposes, we only want a list of sizes, so we can limit the query further by changing the SELECT clause to:

    SELECT DISTINCT size...
    

    so, now we’re only looking at the “size” column, and we use the DISTINCT keyword to avoid duplicate values

    so now, the query returns just the list: (13, 14, 15)

    We make a similar query to get the next 50 smaller items:

    SELECT DISTINCT size FROM products WHERE size < 12 ORDER BY size DESC LIMIT 50
    

    this is just the same as the above query, but we limit to only sizes that are smaller, and we reverse the ordering, so we get the 50 biggest items that are smaller than the desired size.

    in this case this query will return the list (11, 10, 9)

    If we put it all together in the outer query using these two lists, we get:

    SELECT
      *
    FROM
      products
    WHERE
      size = 12 OR
      size IN (13, 14, 15) OR
      size IN (11, 10, 9)
    

    So we pull all the products that have a size of 9 to 15

    I hope this makes sense 🙂

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

Sidebar

Related Questions

Here the problem. I have an ItemsControl , and I want to show a
Here is the code: create table `team`.`User`( `UserID` bigint NOT NULL AUTO_INCREMENT , `Username`
Here is my problem : I have a post controller with the action create.
Here is the css: #content ul { font-size: 12px; } I am trying this:
Here my problem: @Assert\Regex( * pattern=/^[A-Za-z0-9][A-Za-z0-9\]*$/, * groups={creation, creation_logged} * ) I'm using the
Here is the problem that I am trying to solve. I have two folders
Here is my program to find all the subsets of given set. To solve
Here is what is supposed to happen: The moment the user chooses an option
Here's the flow that I am trying to achieve: 1) User uploads an audio
Here is another spoj problem that asks how to find the number of distinct

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.