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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T04:46:27+00:00 2026-05-30T04:46:27+00:00

I have a parts list with 1000 rows. The two fields are Part Number

  • 0

I have a parts list with 1000 rows. The two fields are “Part Number” and “Cost”. The costs range from $1 to $2000 (integers all).

  • A034, 1
  • A012, 5
  • A084, 10
  • B309, 13
  • A094, 25
  • A370, 50
  • A233, 75
  • A343, 75
  • C124, 78
  • …
  • D239, 500
  • …
  • X998, 1980
  • Z901, 2000

I want to create a list of all combinations of parts whose combined cost falls within a tight range (the gap of the range will never be more than $50). For example, given a range of $70-75, the returned list would be:

  • A343 (total $75)
  • A233 (total $75)
  • A370, A094 (total $75)
  • A370, B309, A084 (total $73)
  • A370, B309, A084, A034 (total $74)

My first thoughts were to iterate through all possible combinations of parts that could meet the criteria (i.e. <= the upper number of the range) and report those combinations whose sums fell within the range. It quickly became obvious that would fail because the number of combinations becomes astronomical pretty quickly. But given that most combinations would not meet the criteria, is this problem reasonably solvable?

Given that the data is in a table in a MySQL database, my preferred solution would be some SQL or Stored Proc, next preferred PHP, finally Javascript.

(ETA: the missed hit discovered by @piotrm)

  • 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-30T04:46:29+00:00Added an answer on May 30, 2026 at 4:46 am

    You have to limit maximum of total cost, or the number of combinations will go up to the sky no matter how you try to find them. In the following example it is limited to 75, but you may try other values to see it you can still find results in a reasonable time.

    You can also adapt this solution to update combinations table on inserts or updates for your main table, letting you get results extremely quick for any range that doesnt exceed your set limit (but slowing inserts obviously as it’s where all the work is done).

    Create tables and trigger:

    CREATE TABLE `total_max75` (
     `id` int(11) NOT NULL AUTO_INCREMENT,
     `parts` varchar(255) NOT NULL,
     `num` int(11) NOT NULL,
     `total` int(11) NOT NULL,
     PRIMARY KEY (`id`),
     KEY `total` (`total`,`num`)
    ); 
    
    CREATE TABLE `newparts` (
     `name` char(4) NOT NULL,
     `price` int(11) NOT NULL,
     PRIMARY KEY (`name`)
    );
    
    DELIMITER //
    CREATE TRIGGER addtotal AFTER INSERT ON newparts
    FOR EACH ROW
    BEGIN
    IF NEW.price <= 75 THEN
       INSERT INTO total_max75 ( parts, num, total )
         SELECT CONCAT( t.parts, ', ', NEW.name), 
           t.num+1, t.total+NEW.price 
       FROM total_max75 t
       WHERE t.total <= 75 - NEW.price AND num < 40;
    
       INSERT INTO total_max75( parts, num, total )
         VALUES( NEW.name, 1, NEW.price );
    END IF;
    END//
    DELIMITER ;
    

    Then populate using:

    INSERT INTO newparts( name, price )
    SELECT part_number, cost FROM yourtable
    WHERE cost <= 75;
    

    or (as test data)

    INSERT INTO newparts( name, price ) VALUES
    ('A012', 5),('A034', 1),('A084', 10),('A094', 25),('A233', 75),
    ('A343', 75),('A370', 50),('B309', 13),('C124', 78);
    

    And finally get your result using:

    SELECT * FROM total_max75 WHERE total BETWEEN 70 AND 75;
    

    You may put any range here with maximum less than 75 (or whatever you set as limit in the table creation part and trigger).

    Results:

    A084, A370, B309        73 (you missed this one in your question)
    A034, A084, A370, B309  74
    A233                    75
    A343                    75
    A094, A370              75
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple page with the parts: a list of items taken from
The Sharepoint 2010 Current User Filter Web Part allows List Web Parts that have
I have a view that list Parts in a htmltable format from a database
I have a ComboBox that I am populating with a list of parts for
I have a linked list that I want to sort part of, eg: std::sort(someIterator,
Actually, this question seems to have two parts: How to implement pattern matching? How
I have a list of images that are pulled from a database and loaded
I have used JQuery UI autocomplete to cut down on the list of parts
Possible Duplicate: Remove duplicates from a List<T> in C# i have a List like
I have a DataGridView bound to a List[of Parts]. The last item in the

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.