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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T04:52:53+00:00 2026-05-26T04:52:53+00:00

I have a table A with food products. I have a table B with

  • 0

I have a table A with food products.

I have a table B with food addons.

Table B has the fields:

  • price
  • added_calories
  • name
  • products

I want to make such query that will get the top 10 (or more) combinations of product and addons that will result the best calories / price ratio . I need to keep the names of the addons and product to display to the user.

I should be able to have several addons on 1 product so that I should try and run through all combinations.

If we have addons a, b, c, d then I have to try the combinations:

  • Product + a
  • Product + b
  • Product + ab
  • Product + ac
  • Product + ad
  • Product + bc
  • Product + bd
  • …

Not every addon can be selected for each product. There’s an additional ‘connector’ table which indicates supported addons for a product.

  • 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-26T04:52:54+00:00Added an answer on May 26, 2026 at 4:52 am

    Assuming that you want to maximize calories / price, I think this is doable. The trick is that, given a specific product, you’ll maximize calories / price by piling on add-ons, in descending order by calories / price ratio, until the total ratio starts to decrease.

    The following query is probably ridiculously inefficient and in need of serious optimization, but at least it shows that a solution exists:

    SELECT
      p.name AS product,
      GROUP_CONCAT(a.name) AS additives,
      p.price + COALESCE(SUM(a.price), 0) AS price,
      p.calories + COALESCE(SUM(a.added_calories), 0) AS calories,
      (p.calories + COALESCE(SUM(a.added_calories), 0)) /
        (p.price + COALESCE(SUM(a.price), 0)) AS calories_per_price
    FROM
      products AS p
      LEFT JOIN connector AS c ON c.product = p.id
      LEFT JOIN additives AS a
        ON a.id = c.additive
        AND (a.added_calories / a.price) > (
          (p.calories + COALESCE((
            SELECT SUM(b.added_calories) FROM connector AS d, additives AS b
            WHERE d.product = p.id AND b.id = d.additive
              AND (b.added_calories / b.price) > (a.added_calories / a.price)
          ), 0)) /
          (p.price + COALESCE((
            SELECT SUM(b.price) FROM connector AS d, additives AS b
            WHERE d.product = p.id AND b.id = d.additive
              AND (b.added_calories / b.price) > (a.added_calories / a.price)
          ), 0))
        )
    GROUP BY p.id
    ORDER BY calories_per_price DESC
    LIMIT 10;
    

    Edit: OK, I debugged it, now it actually works(!). Here’s some test data:

    INSERT INTO products (id, name, calories, price) VALUES
      (1, 'Cardboard', 0, 1),
      (2, 'Lard', 1000, 100),
      (3, 'Spaghetti', 10, 50);
    INSERT INTO additives (id, name, added_calories, price) VALUES
      (1, 'Salt', 0, 2),
      (2, 'Butter', 500, 100),
      (3, 'Cheese', 300, 70),
      (4, 'Pepper', 0, 3),
      (5, 'Ketchup', 50, 10),
      (6, 'Milk', 20, 10);
    INSERT INTO connector (product, additive) VALUES
      (1,1), (1,2), (1,3), (1,4), (1,5), (1,6),
      (2,1), (2,3), (2,4), (2,5),
      (3,1), (3,2), (3,3), (3,4), (3,5);
    

    And results:

    +-----------+-----------------------+-------+----------+--------------------+
    | product   | additives             | price | calories | calories_per_price |
    +-----------+-----------------------+-------+----------+--------------------+
    | Lard      | NULL                  |   100 |     1000 |                 10 | 
    | Cardboard | Butter,Ketchup        |   111 |      550 |   4.95495495495495 | 
    | Spaghetti | Butter,Cheese,Ketchup |   230 |      860 |   3.73913043478261 | 
    +-----------+-----------------------+-------+----------+--------------------+
    

    Edit 2: Oops, I had it maximizing price per calories instead of calories per price. Fixed.

    Edit 3: The subqueries were ignoring the connector table. I’ve (hopefully) fixed that bug, but I haven’t been able to test it yet.

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

Sidebar

Related Questions

I have table with 50 entries (users with such details like Name Surname Location
I have table A in Oracle that has a primary key (id). I need
I have a products table, with the fields productname, category and cost, of type
I have a products table, with the fields product, category and cost, of type
I have a table that consists of the following: food || last_order_date ------------------------- pizza
I have three tables that hold items like: Products: ID NAME CATEGORY STOCK 1
I have a table that looks something like: Favourite color | Favourite Food |
I have table definition like below: Place (id, name) Review (id, userid, placeid) Favorite
I have table inside a div tab. The table has 40 rows in it
i have table structure with 3 colums (column1, column2, column3) and i want to

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.