当前位置: 代码迷 >> Sql Server >> 排列组合实现有关问题
  详细解决方案

排列组合实现有关问题

热度:50   发布时间:2016-04-24 10:55:37.0
排列组合实现问题
有下表数据
orderid     itemname
1                     a
1                     b
1                     c
1                     d
2                     e
2                     f
2                     g 

用SQL怎么实现下面排列组合的结果

rid         items
1            a,b
2            a,c
3            a,d
4            b,c
5            b,d
6            c,d
7            e,f
8            e,g
9             f,g 

------解决方案--------------------
create table #tb(id int,a varchar(10))
insert into #tb 
select

1           ,          'a' union all
select 1           ,          'b' union all
select 1           ,          'c' union all
select 1           ,          'd' union all
select 2           ,          'e' union all
select 2           ,         'f' union all
select 2           ,          'g' 


select b.a+a.a,* from #tb a full join #tb b on a.id=b.id 
where a.a<>b.a

不知道是不是这样