select a,sum(case when b=1 then 1 else 0 end) [1],sum(case when b=2 then 1 else 0 end) [2] from T group by a