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

  • Home
  • SEARCH
  • 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 92339
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T23:10:17+00:00 2026-05-10T23:10:17+00:00

EDIT: I’m told that making you guys read means I get less attention. My

  • 0

EDIT:

I’m told that making you guys read means I get less attention. My apologies. Here’s a simpler version:

Bill got $100 dollars worth of items from a store.

He wants to return enough of the items to get exactly $30 dollars back.

The store has a Point of Return system that will help him do this.

Here is the data after he scans his items:

       item ¦   price ¦  socks             4.00 cheap tv         22.00 book on tape      9.00 book on paper     7.00 party hats        3.00 picture frame    10.00 hammer            5.00 juicer           16.00 mysql guide      24.00  total items  ¦ total price ¦             9   100.00  Option 1 =============== item ¦          price ¦ cheap tv        22.00 party hats       3.00 hammer           5.00 ===============  Option 2 =============== item ¦          price ¦  socks            4.00 picture frame   10.00 juicer          16.00 ===============  Option 3 =============== item ¦          price ¦  book on tape    9.00 hammer          5.00 juicer         16.00 

I probably missed a few options, since I made all of this up.

So, the big question is:

Is there a way (with GROUP BY, probably) to have one query that would return ever possible combination of items?

Thanks!

a

  • 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. 2026-05-10T23:10:18+00:00Added an answer on May 10, 2026 at 11:10 pm

    If the number of items are small enough you can brute force this with SQL. This might be a quick to write solution, but you probably want to do something smarter. Sounds like the ‘knapsack problem‘ which is NP complete.

    If the number of items is large, you will need to delve into dynamic programming algorithms. You have to ask yourself how important this is to your application.

    If the number of items is relatively small, you may be able to brute-force this. A brute-force SQL statement (which is what you asked for) that finds combinations of 1,2 or 3 items that match is as follows. If this is not satisfactory, then maybe SQL is not the right tool for this job.

    SELECT    i1.id AS id1,    NULL AS id2,    NULL AS id3,    i1.amount FROM    items i1 UNION ALL SELECT    i1.id AS id1,    i2.id AS id2,    i3.id AS id3,    i1.amount + i2.amount AS total FROM    items i1,    items i2 WHERE    i1.amount + i2.amount = 30 AND    i1.id <> i2.id AND    i1.id <> i3.id UNION ALL SELECT    i1.id AS id1,    i2.id AS id2,    i3.id AS id3,    i1.amount + i2.amount + i3.amount AS total FROM    items i1,    items i2,    items i3 WHERE    i1.amount + i2.amount + i3.amount = 30 AND    i1.id <> i2.id AND    i1.id <> i3.id AND    i2.id <> i3.id 

    In Oracle, you would use the CUBE function to turn this into a generic version, not sure about a MySQL equivalent.

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

Sidebar

Ask A Question

Stats

  • Questions 77k
  • Answers 78k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer You don't care about the user's connection type, you care… May 11, 2026 at 3:37 pm
  • added an answer If the id field is an autogenerated (identity/guid) field, the… May 11, 2026 at 3:37 pm
  • added an answer You can use the attribute ends with selector: $('tag[id$='ServerSideId']') Where… May 11, 2026 at 3:37 pm

Related Questions

I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
Edit: I have solved this by myself. See my answer below I have set
EDIT: I missed a crucial point: .NET 2.0 Consider the case where I have
EDIT: I'm still waiting for more answers. Thanks! In SQL 2000 days, I used
Edit: I am using SqlDataAdapters to fill the data sets. Sorry--I should have been

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.