select a,sum(case when c=111 then 1 else 0 end) as [111的个数],
  sum(case when c=222 then 1 else 0 end) as [222的个数],
  sum(case when c=333 then 1 else 0 end) as [333的个数]
from tab
group by a

解决方案 »

  1.   

    动态的如下:
    create table #t(A varchar(10),B varchar(10),C varchar(10))
    insert into #t(A,B,C)
    select   'me',    'D',   '111' union all
    select   'she',   'k',   '222' union all
    select   'me',    'f',   '111' union all
    select   'me',    'e',   '333'declare @sql varchar(8000)
    set @sql=''
    select @sql=@sql+',sum(case when c='''+c+''' then 1 else 0 end) ['+c+'的个数]'
      from #t group by c
    exec('select a'+@sql+' from #t group by a')
    drop table #t