--> 测试数据: #T
if object_id('tempdb.dbo.#T') is not null drop table #T
create table #T (id int,data varchar(1))
insert into #T
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' union all
select 3,'h'--> 2005
select id, data=(stuff((select ','+data from #T where id=a.id for xml path('')),1,1,'')) from #T a group by id
/*
id          data
----------- -----------
1           a,b,c,d
2           e,f,g
3           h
*/--> 2000
declare @i int, @max int, @sql varchar(max)
select top 1 @i=1, @max=count(*) from #T group by id order by 2 desc
while @i<=@max
begin
set @sql=isnull(@sql+'+max(case i when '+ltrim(@i)+' then '',''+','max(case i when '+ltrim(@i)+' then ')+'data else '''' end)'
set @i=@i+1
end
exec ('select id, data='+@sql+' from (select *,i=(select count(1) from #T where id=a.id and data<=a.data) from #T a) t group by id')
/*
id          data
----------- -----------
1           a,b,c,d
2           e,f,g
3           h
*/

解决方案 »

  1.   

    多谢,可能我表达的不够清楚。
    有一个表是这样的
      列1  列2
      1   a 
      1   b 
      1   c 
      1   d 
      2   e 
      2   f 
      2   g  
      3   h 
    要得到的表
      列1 列2 列3  列4  列5
      1   a   b    c   d
      2   e   f    g
      3   h
    有没有简单的方法,除了用循环。
      

  2.   


    --> 测试数据: #T
    if object_id('tempdb.dbo.#T') is not null drop table #T
    create table #T (id int,data varchar(1))
    insert into #T
    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' union all
    select 3,'h'
    select id,
           max( case seq when 1 then data else '' end ) + ', ' +     
           max( case seq when 2 then data else '' end ) + ', ' +     
           max( case seq when 3 then data else '' end ) + ', ' +     
           max( case seq when 4 then data else '' end )  as data
    from (
           select T1.id,T1.data,
          ( select count(*) from #T T2 where T2.id= T1.id and T2.data<=T1.data ) as seq from #T T1 ) D
     group by id
    /*
    id          data
    ----------- ----------
    1           a, b, c, d
    2           e, f, g, 
    3           h, , , (3 row(s) affected)
    */
      

  3.   


    select id,
           max( case seq when 1 then data else '' end ) as col1,     
           max( case seq when 2 then data else '' end ) as col2,   
           max( case seq when 3 then data else '' end ) as col3,   
           max( case seq when 4 then data else '' end )as col4
    from (
           select T1.id,T1.data,
          ( select count(*) from #T T2 where T2.id= T1.id and T2.data<=T1.data ) as seq from #T T1 ) D
     group by id
    /*
    id          col1 col2 col3 col4
    ----------- ---- ---- ---- ----
    1           a    b    c    d
    2           e    f    g    
    3           h              (3 row(s) affected)
    */     
      

  4.   

    数据  
      1 a 
      1 b 
      1 c 
      1 d 
      2 e 
      2 f 
      2 g 
      3 h 
    结果 
      1 a b c d 
      2 e f g 
      3 h多谢,可能我表达的不够清楚。 
    有一个表是这样的 
      列1  列2 
      1   a  
      1   b  
      1   c  
      1   d  
      2   e  
      2   f  
      2   g   
      3   h  
    要得到的表 
      列1 列2 列3  列4  列5 
      1   a   b    c   d 
      2   e   f    g 
      3   h 
    有没有简单的方法,除了用循环。 
    ----------------------------------------select 列1,
      max(case px when 1 then 列2 else '' end ) 列2,
      max(case px when 2 then 列2 else '' end ) 列3,
      max(case px when 3 then 列2 else '' end ) 列4,
      max(case px when 4 then 列2 else '' end ) 列5
    from
    (
      select * , px = (select count(1) from tb where 列1 = 列1 and 列2 < t.列2) + 1 from tb t
    ) m
    group by 列1
      

  5.   

    create table tb(列1 int , 列2 varchar(10)) 
    insert into tb values(1 ,'a')  
    insert into tb values(1 ,'b')  
    insert into tb values(1 ,'c') 
    insert into tb values(1 ,'d')  
    insert into tb values(2 ,'e')  
    insert into tb values(2 ,'f')  
    insert into tb values(2 ,'g')  
    insert into tb values(3 ,'h')  
    goselect 列1,
      max(case px when 1 then 列2 else '' end ) 列2,
      max(case px when 2 then 列2 else '' end ) 列3,
      max(case px when 3 then 列2 else '' end ) 列4,
      max(case px when 4 then 列2 else '' end ) 列5
    from
    (
      select * , px = (select count(1) from tb where 列1 = t.列1 and 列2 < t.列2) + 1 from tb t
    ) m
    group by 列1drop table tb/*
    列1          列2         列3         列4         列5         
    ----------- ---------- ---------- ---------- ---------- 
    1           a          b          c          d
    2           e          f          g          
    3           h                                (所影响的行数为 3 行)
    */
    /*
    ID          BB         CC          
    ----------- ---------- ----------- 
    1           0001       1
    2           0002       2
    3           0003       3
    4           0004       4
    5           0005       5
    6           0006       6(所影响的行数为 6 行)
    */
      

  6.   

    --上面后面部分不要.create table tb(列1 int , 列2 varchar(10)) 
    insert into tb values(1 ,'a')  
    insert into tb values(1 ,'b')  
    insert into tb values(1 ,'c') 
    insert into tb values(1 ,'d')  
    insert into tb values(2 ,'e')  
    insert into tb values(2 ,'f')  
    insert into tb values(2 ,'g')  
    insert into tb values(3 ,'h')  
    goselect 列1,
      max(case px when 1 then 列2 else '' end ) 列2,
      max(case px when 2 then 列2 else '' end ) 列3,
      max(case px when 3 then 列2 else '' end ) 列4,
      max(case px when 4 then 列2 else '' end ) 列5
    from
    (
      select * , px = (select count(1) from tb where 列1 = t.列1 and 列2 < t.列2) + 1 from tb t
    ) m
    group by 列1drop table tb/*
    列1          列2         列3         列4         列5         
    ----------- ---------- ---------- ---------- ---------- 
    1           a          b          c          d
    2           e          f          g          
    3           h                                (所影响的行数为 3 行)
    */