In a post I found that I can use GROUP BY CUBE
to find all possible combinations:
select concat(a,b,c,d)
from (select 'a','b','c','d') as t(a,b,c,d)
group by cube(a,b,c,d)
having len(concat(a,b,c,d)) = 3
The code is pretty because is very easy to understand.
I would like to use the same code but with int
instead of char
. Basically I would like to find all possible combinations of numbers (1,2,3,4).
The goal is to sum them and generate all possible totals:
- 1 + 2 + 3 = 6
- 2 + 3 + 4 = 7
- 3 + 4 + 1 = 8
- etc..
I'm trying to resolve a knapsack problem in T-SQL and I would like to see if GROUP BY CUBE
can be a solution