SQL Server 那些行的合计数等于一个指定数

已知一个数,得知那些行的合计后等于这个数:如,表中哪些行数相加等于200的全部组合

CREATE TABLE #t(ID VARCHAR(1),数据 INT)
INSERT INTO #t
select 'a',10 union all
select 'b',90 union all
select 'c',100 union all
select 'd',180 union ALL
select 'e',15 union ALL
select 'f',5;
with ps as
(
select *,path=cast(id as varchar(8000)),path1=cast(数据 as varchar(8000)),total=数据 from #t
union all
select b.id,b.数据,a.path+','+b.id,path1+','+ltrim(b.数据), a.total+b.数据
from ps a join #t b on a.id<b.id and a.total<200
)

select path,path1,total from ps where total=200

 

posted @ 2020-11-16 16:31  VicLW  阅读(664)  评论(0编辑  收藏  举报