"
Ask TOM
Questions \ How to do a combination to sum to find all the options up to a certain value
Whilst you are here, check out some content from the AskTom team: Histograms on data
(not the optimizer)
You Asked
Hello TomConChris
I can have up to 7 (in reality 50) values, and I must sum all the combination and choose all the combination that get an
amount
In example All the combinations with the numbers to get a pizza of 5 kg.
option 1 5Kg,
Option 2 3Kg
Option 3 2Kg
Option 4 4Kg
In the previous example I had tow combionatios (option1 5=5) (options2 and option3 2+3=5) both givesme 5.
In the following example I have a way to get all the combinations, in the previous case (option2 +option3=5), but I can't find
the option1=5, because all the combinations returns a value, and I need always they include the option, I had done a several
test and I couldn't then I thought why to do it, if TomConChris can :)
WITH COMBINACION AS
( SELECT 1 C, 1 Q
FROM DUAL
UNION ALL
SELECT 2 C, 2 Q
FROM DUAL)
SELECT T1.C C1,T2.C C2,T3.C C3,T4.C C4,T5.C C5,T6.C C6,T7.C C7
FROM COMBINACION T1
https://asktom.oracle.com/pls/asktom/f?p=100:11:0::::P11_QUESTION_ID:9531157800346395886 1/5
9/14/2019 Ask TOM "How to do a combination to sum to find all the opt..."
This resturns
12
21
I need it inclues all the times a 0 in all the combinations, to get the situations when only one is enough (in reality it has 50
elements)
10
12
20
21
I would need
100
120
123
020
etc.
I tried things like LEFT OUTER JOIN COMBINACION T2 ON (T2.C NOT IN (T1.C) OR T2.C=0) , but I think I'm misunderstanding
the working of the LEFT OUTER JOIN, because I never used, truely.
and we said...
Eeek! Joining like that limits the number of things you can compare to the number of joins. And it's terrible for performance
as your tables have more rows.
https://asktom.oracle.com/pls/asktom/f?p=100:11:0::::P11_QUESTION_ID:9531157800346395886 2/5
9/14/2019 Ask TOM "How to do a combination to sum to find all the opt..."
Ask
This will maintain two things TOM
Questions Office
weight
- The total Hours
so far Resources About
- A list of the pizza toppings you've already checked
Obviously we need the weight, because that's what we're measuring. The list is to prevent us checking the same row twice.
So we want to start with all the rows in the table. You could exclude those <= 5kg at this point if necessary. Wrap the
topping with hashes. This ensures 1 is not confused with 11, 21, etc. (#1# <> #11#, but 1# is part of 11#, 21# and so on).
For each row that passes these checks, append its topping to the list of those already processed and increment the total
weight:
create table t (
topping int,
weight int
);
Questions
TOPPINGS Office
TOT_WT Hours Resources About
#1# 5
#2# 3
#3# 2
#4# 4
#2#3# 5
This gets you all the combinations that weight up to 5kg. Add a 1kg option and it still works:
#1# 5
#2# 3
#3# 2
#4# 4
#5# 1
#2#3# 5
#2#5# 4
#3#5# 3
#4#5# 5
If you want just those that are exactly 5kg, add this to the final where clause!
Is this answer out of date? If it is, please let us know via a Review
Reviews
Thank you :)
August 11, 2016 - 3:44 pm UTC
Reviewer: Reyes from Bolivia
https://asktom.oracle.com/pls/asktom/f?p=100:11:0::::P11_QUESTION_ID:9531157800346395886 4/5