October 21, 2024
Chicago 12, Melborne City, USA
SQL

Splitting a list of numbers into two groups with it's best combination possible without splitting a number


I want to split a list of quantities between two groups of peoples without splitting any single quantity.

The goal is to split the quantities into two groups using the best combinations possible.

Please find below an example:

List of quantities:

The quantities are in different rows as below.

ID Quantities
1 30
2 36
3 5
4 23
5 1
6 4
7 1

I want to find the way to split these quantities into two halves (Between Group A and B) where the sum of quantities in each group can be exactly equal or can be some less than other group.

In the above mentioned example, I can split the quantities as

  1. Group A: 30,36 = 66 ; Group B: 23,5,4,1,1 = 34

    (or)

  2. Group A: 30,23 = 53 ; Group B: 36,5,4,1,1 = 47

    Among the above two possible ways, 2nd combination is the best. Like wise I want to find the possible combinations and pick the best of all.

    Can someone please help me with the logic to solve the above mentioned math using a SQL query?

Thanks in advance.



You need to sign in to view this answers

Leave feedback about this

  • Quality
  • Price
  • Service

PROS

+
Add Field

CONS

+
Add Field
Choose Image
Choose Video