表是这样的一个 需要行转列
create table aaa (id int,id2 int,num varchar(4))
go
insert into aaa select 1,2,'a'
union all select 1,3,'b'
union all select 1,5,'c'
union all select 1,8,'d'
union all select 2,9,'e'
union all select 2,11,'f'
union all select 2,23,'g'
union all select 2,24,'h'
union all select 4,23,'i'select * from aaa
go
drop table aaa---------------------------
想要的结果是 
id id2x1 id2x2 id2x3 id2x4
1  a     b     c     d
2  e     f     g     h
4  i就是把id相同 id2不相同 组成一行。。
如果是平时的 case when id2= xx 就可以。现在id2不是固定的。只是想把id相同的横向显示一下 id2x1 列就是第一行id=2的数据,id2x2 列就是第二行id=2的数据,
id3x1 列就是第三行id=2的数据。
最多不会超过7列。

解决方案 »

  1.   

    create table aaa (id int,id2 int,num varchar(4))
    go
    insert into aaa select 1,2,'a'
    union all select 1,3,'b'
    union all select 1,5,'c'
    union all select 1,8,'d'
    union all select 2,9,'e'
    union all select 2,11,'f'
    union all select 2,23,'g'
    union all select 2,24,'h'
    union all select 4,23,'i'
    godeclare @i int,@sql varchar(8000)
    select @i=max(num),@sql='' from (select count(*) as num from aaa group by id) twhile @i>0
    begin
        select @sql=',id2x'+rtrim(@i)+'=max(case i when '+rtrim(@i)+' then num end)'+@sql,@i=@i-1
    endset @sql='select id'+@sql+' from (select *,(select count(*) from aaa where id=a.id and id2<=a.id2) as i from aaa a) t group by id'
    exec(@sql)
    go/*
    id          id2x1 id2x2 id2x3 id2x4 
    ----------- ----- ----- ----- ----- 
    1           a     b     c     d
    2           e     f     g     h
    4           i     NULL  NULL  NULL
    */drop table aaa
    go
      

  2.   

    create table aaa (id int,id2 int,num varchar(4))
    go
    insert into aaa select 1,2,'a'
    union all select 1,3,'b'
    union all select 1,5,'c'
    union all select 1,8,'d'
    union all select 2,9,'e'
    union all select 2,11,'f'
    union all select 2,23,'g'
    union all select 2,24,'h'
    union all select 4,23,'i'
    godeclare @i int,@sql varchar(8000)
    select @i=max(num),@sql='' from (select count(*) as num from aaa group by id) twhile @i>0
        select @sql= ',id2x'+rtrim(@i)
                    +'=isnull(max(case i when '+rtrim(@i)+' then num end),'''')'
                    +@sql,
               @i  = @i-1set @sql='select 
                  id'+@sql+' 
              from 
                  (select 
                       a.*,
                       (select count(*) from aaa where id=a.id and id2<=a.id2) as i 
                   from 
                       aaa a) t 
              group by 
                  id'
    exec(@sql)
    go/*
    id          id2x1 id2x2 id2x3 id2x4 
    ----------- ----- ----- ----- ----- 
    1           a     b     c     d
    2           e     f     g     h
    4           i                 
    */drop table aaa
    go
      

  3.   

    create table aaa(id int,id2 int,num varchar(4))
    insert into aaa select 1,2,'a'
    union all select 1,3,'b'
    union all select 1,5,'c'
    union all select 1,8,'d'
    union all select 2,9,'e'
    union all select 2,11,'f'
    union all select 2,23,'g'
    union all select 2,24,'h'
    union all select 4,23,'i'
    create table #aaa (id int,id2 int,num varchar(4),ids int)
    insert #aaa(id,id2,num)
    select * from aaa order by id
    Go
    declare @ids int,@id int
    update #aaa
    set ids=@ids,@ids=(case id when @id then @ids+1 else 1 end),@id=id
    Go
    declare @sql varchar(8000)
    set @sql = 'select id'
    select @sql = @sql + ',max(case ids when '''+cast(ids as varchar(10))+''' then num else '''' end) [id2x'+cast(ids as varchar(10))+']'
     from (select distinct ids from #aaa) as a
    select @sql = @sql+' from #aaa group by id'
    select @sql
    exec(@sql)
      

  4.   

    create table aaa (id int, id2 int, num varchar(4))
    go
    insert into aaa select 1,2,'a'
    union all select 1,3,'b'
    union all select 1,5,'c'
    union all select 1,8,'d'
    union all select 2,9,'e'
    union all select 2,11,'f'
    union all select 2,23,'g'
    union all select 2,24,'h'
    union all select 4,23,'i'declare @sql varchar(8000)
    set @sql='select id,'
    select @sql=@sql+quotename('id2X'+rtrim(id3))+'=max(case when id3='+rtrim(id3)+' then num else '''' end),' from #T group by id3
    select @sql=left(@sql, len(@sql)-1), @sql=@sql+' from #T group by id '
    exec(@sql)--result
    id          id2X1 id2X2 id2X3 id2X4 
    ----------- ----- ----- ----- ----- 
    1           a     b     c     d
    2           e     f     g     h
    4           i
      

  5.   

    --改改, 上面的少了一句
    --創建表
    create table aaa (id int, id2 int, num varchar(4))
    go
    insert into aaa select 1,2,'a'
    union all select 1,3,'b'
    union all select 1,5,'c'
    union all select 1,8,'d'
    union all select 2,9,'e'
    union all select 2,11,'f'
    union all select 2,23,'g'
    union all select 2,24,'h'
    union all select 4,23,'i'
    --創建臨時表
    select *, id3=(select count(*) from aaa where id=A.id and id2<=A.id2) into #T from aaa as A
    --生成動態SQL
    declare @sql varchar(8000)
    set @sql='select id,'
    select @sql=@sql+quotename('id2X'+rtrim(id3))+'=max(case when id3='+rtrim(id3)+' then num else '''' end),' from #T group by id3
    select @sql=left(@sql, len(@sql)-1), @sql=@sql+' from #T group by id '
    exec(@sql)
    --result
    id          id2X1 id2X2 id2X3 id2X4 
    ----------- ----- ----- ----- ----- 
    1           a     b     c     d
    2           e     f     g     h
    4           i      
    --刪除表
    drop table aaa
    drop table #T