有下面5列的数据,如何才能实现从每行分别取出2个、3个数进行重新组合, b1 b2 b3 b4 b5
a1 7 16 23 28 31
a2 13 19 20 22 33
a3 3 11 13 20 35
a4 27 31 33 34 35
a5 9 19 22 29 30
a6 3 5 28 29 33
a7 1 13 16 18 32
a8 10 17 22 24 32
a9 6 13 18 20 33
a10 3 11 14 25 34
想要得到下面的结果:
每行取2个数的
b1 b2
a1 7 16
a1 7 23
a1 7 28
a1 7 31
a1 16 23
a1 16 28
a1 16 31

a2
a2

每行取3个数的
b1 b2 b3
a1 7 16 23
a1 7 23 28
a1 7 28 31
a1 16 23 28
a1 16 28 31
a1 23 28 31

a2
a2

假如数据为6列,分别取2个、3个数、4个数进行重新组合,又该如何呢?谢谢大虾
b1 b2 b3 b4 b5 b6
a1 7 16 23 28 31 3
a2 13 19 20 22 33 5
a3 3 11 13 20 35 3
a4 27 31 33 34 35 4
a5 9 19 22 29 30 2
a6 3 5 28 29 33 4
a7 1 13 16 18 32 7
a8 10 17 22 24 32 3
a9 6 13 18 20 33 9
a10 3 11 14 25 34 1

解决方案 »

  1.   

    with cte as(
    select aa,b1 from test11
    union 
    select aa,b2 from test11
    union 
    select aa,b3 from test11
    union 
    select aa,b4 from test11
    union 
    select aa,b5 from test11)
    select a.*,b.b1 from cte a,cte b
    where a.aa=b.aa and a.b1>b.b1
    order by aa,a.b1with cte as(
    select aa,b1 from test11
    union 
    select aa,b2 from test11
    union 
    select aa,b3 from test11
    union 
    select aa,b4 from test11
    union 
    select aa,b5 from test11)
    select a.*,b.b1,c.b1 from cte a,cte b,cte c
    where a.aa=b.aa and a.b1>b.b1 and a.aa=c.aa and b.b1>c.b1
    order by aa,a.b1
      

  2.   


    create table TestTb
    (b1 nvarchar(10),b2 int, b3 int,b4  int,b5 int,b6 int,b7 int)
    insert TestTb
    select 'a1', 7 ,16, 23, 28, 31,87 union all
    select 'a2', 13 ,19 ,20 ,22, 33,45 union all
    select 'a3', 3 ,11 ,13 ,20, 35,42 union all
    select 'a4', 27 ,31 ,33, 34, 35,39 union all
    select 'a5', 9 ,19 ,22, 29, 30,35 union all
    select 'a6', 3 ,5 ,28, 29, 33,32 union all
    select 'a7', 1 ,13 ,16 ,18 ,32,31 union all
    select 'a8', 10 ,17 ,22 ,24, 32,34 union all
    select 'a9', 6 ,13 ,18, 20, 33,33 union all
    select 'a10', 3 ,11 ,14, 25 ,34,66declare @count as int
    set @count=3   --這裡改成你想要的組合數2、3、4、5...... if @count>1 and @count<=(select count(1) from sys.columns where [object_id]=object_id('TestTb') and [name]<>'b1')
    begindeclare @sql as nvarchar(4000)
    declare @col_Str nvarchar(500)
    declare @form_Str nvarchar(500)
    declare @i as int
    set @col_Str=' t1.ColName+'',''+t2.ColName'
    set @form_Str=' join T1 as t2 on t2.num>t1.num '
    set @i=3
    while  @i<=@count
    begin
       set @col_Str=@col_Str+'+'',''+t'+ltrim(@i)+'.ColName'
       set @form_Str=@form_Str+' join T1 as t'+ltrim(@i)+' on t'+ltrim(@i)+'.num>t'+ltrim(@i-1)+'.num '
       set @i=@i+1
    endset @sql=' declare @sql as nvarchar(4000)'+
             ' set @sql='''''+
             ' ;with T1 as(select row_number()over(order by [name]) as num,'+
             ' [name] as ColName from sys.columns where [object_id]=object_id(''TestTb'') and [name]<>''b1'')'+
             ' ,T2 as (select '+@col_Str+' as string from T1 as t1 '+@form_Str+')'+
             ' select @sql=@sql+'' union all select b1,''+string+'' from TestTb'' from t2'+
             ' set @sql=''select * from (''+stuff(@sql,1,10,'''')+'') as t order by b1'''+
             ' exec(@sql)'
    exec(@sql)end
    試著弄了弄~~