SQL获取所有可能的数字组合

时间:2019-01-24 15:20:52

标签: sql flags pow

我有一个这样的桌子

 | Number |
 ----------
 | 1      |
 | 2      |
 | 4      |
 | 8      |
 | 16     |
 | 32     |

可以创建一个函数来返回

| 1 | 2 | 3 | 4 | 5  | 6  | sum |
 --------------------------------
| 1 |   |   |   |    |    | 1   |
| 1 | 2 |   |   |    |    | 3   |
| 1 | 2 | 4 |   |    |    | 7   |
| 1 | 2 | 4 | 8 |    |    | 15  |
| 1 | 2 | 4 | 8 | 16 |    | 31  |
| 1 | 2 | 4 | 8 | 16 | 32 | 63  |

3 个答案:

答案 0 :(得分:1)

一个有趣的请求,这是MSSQL的解决方案。如果您更清楚地说明您的主要目的,我们将提供更好的解决方案。

create table #temp (num int)
insert into #temp (num) values (1),(2),(4),(8),(16),(32)

select num, rn = ROW_NUMBER() OVER (order by num asc)
into #temp2
from #temp

select num,num2=null,num3=null,num4=null,num5=null,num6=null,Total=(select sum(num) from #temp2 where rn in (1)) from #temp2 where rn = 1
union
select null,num,null,null,null,null,Total=(select sum(num) from #temp2 where rn in (1,2)) from #temp2 where rn = 2
union
select null,null,num,null,null,null,Total=(select sum(num) from #temp2 where rn in (1,2,3)) from #temp2 where rn = 3
union
select null,null,null,num,null,null,Total=(select sum(num) from #temp2 where rn in (1,2,3,4)) from #temp2 where rn = 4
union
select null,null,null,null,num,null,Total=(select sum(num) from #temp2 where rn in (1,2,3,4,5)) from #temp2 where rn = 5
union
select null,null,null,null,null,num,Total=(select sum(num) from #temp2 where rn in (1,2,3,4,5,6)) from #temp2 where rn = 6
order by Total asc

答案 1 :(得分:0)

返回的列数是否动态?如果是,那么我认为无法通过SQL函数来完成。

如果仅查找最后一列“ sum”,则可以使用下面的代码。

SELECT power(2,RowID)-1

RowID是顺序行号。

答案 2 :(得分:-1)

这是我要解决的问题。我最初认为您可以使用Apply运算符,但是联合方法最易于使用。我的答案和Eray(我真的很喜欢这种方法)的主要区别在于,我在生成和值的行中使用了不相关的子查询,并保持每个列中的唯一数字。

我认为可以通过使用动态SQL来实现未知数量的列来实现此目的,但是我没有时间或资源来尝试解决这个问题。请参见下面的代码,希望对您有所帮助。

declare @numbers table
(
    numberid int
);

insert into @numbers (numberid)
values
(1),
(2),
(4),
(6),
(8),
(16),
(32);


select numberid as [1], null as [2], null as [3], null as [4], null as [5], null as 
[6], (select sum(numberid) from @numbers where numberid=1) as [sum] from @numbers 
where numberid =1
union
select (select numberid from @numbers where numberid=1), numberid, null, null, null, 
null, (select sum(numberid) from @numbers where numberid in (1, 2)) from @numbers 
where numberid =2
union
select (select numberid from @numbers where numberid=1), (select numberid from 
@numbers where numberid=2), numberid, null, null, null, (select sum(numberid) from 
@numbers where numberid in (1, 2, 4)) from @numbers where numberid =4
union
select (select numberid from @numbers where numberid=1), (select numberid from 
@numbers where numberid=2), (select numberid from @numbers where numberid=4), 
numberid, null, null, (select sum(numberid) from @numbers where numberid in (1, 2, 4, 
8)) from @numbers where numberid =8
union
select (select numberid from @numbers where numberid=1), (select numberid from 
@numbers where numberid=2), (select numberid from @numbers where numberid=4), (select 
numberid from @numbers where numberid=8), numberid, null, (select sum(numberid) from 
@numbers where numberid in (1, 2, 4, 8, 16)) from @numbers where numberid =16
union
select (select numberid from @numbers where numberid=1), (select numberid from 
@numbers where numberid=2), (select numberid from @numbers where numberid=4), (select 
numberid from @numbers where numberid=8), (select numberid from @numbers where 
numberid=16), numberid, (select sum(numberid) from @numbers where numberid in (1, 2, 
4, 8, 16, 32)) from @numbers where numberid =32;