select [id],[名稱1]=max(case 名稱 when 'aa'  then 名稱 end),
[名稱2]=max(case 名稱 when 'bb'  then 名稱 end),
[名稱3]=max(case 名稱 when 'cc'  then 名稱 end)
from table1 group by [id]

解决方案 »

  1.   

    create table tbname(id int,名稱 varchar(20))
    insert tbname
    select 1,'aa' union all
    select 1,'bb' union all
    select 1,'cc' union all
    select 2,'dd' union all
    select 2,'ee'
    goselect  a.[id],[名稱1]=min(名稱),
    aa2=(case  when count(1)>1 then (select top 1 名稱 from tbname where id=a.id and 名稱>min(a.名稱) 
    order by 名稱 ) else null end),
    aa3=(case count(1) when 3 then (select top 1 名稱 from tbname where id=a.id order by 名稱 desc) else null end)
    from tbname a group by [id]/*
    id          名稱1                  aa2                  aa3                  
    ----------- -------------------- -------------------- -------------------- 
    1           aa                   bb                   cc
    2           dd                   ee                   NULL(所影响的行数为 2 行)
    */drop table tbname