select
[列1中a的个数] = sum(case 列1 when 'a' then 1 else 0 end),
[列1中b的个数] = sum(case 列1 when 'b' then 1 else 0 end),
[列2中是的个数] = sum(case 列2 when '是' then 1 else 0 end),
[列2中副的个数] = sum(case 列2 when '否' then 1 else 0 end),
[列3中1的个数] = sum(case 列3 when '1' then 1 else 0 end),
[列3中2的个数] = sum(case 列3 when '2' then 1 else 0 end),
[列3中3的个数] = sum(case 列3 when '3' then 1 else 0 end)
from
表
[列1中a的个数] = sum(case 列1 when 'a' then 1 else 0 end),
[列1中b的个数] = sum(case 列1 when 'b' then 1 else 0 end),
[列2中是的个数] = sum(case 列2 when '是' then 1 else 0 end),
[列2中副的个数] = sum(case 列2 when '否' then 1 else 0 end),
[列3中1的个数] = sum(case 列3 when '1' then 1 else 0 end),
[列3中2的个数] = sum(case 列3 when '2' then 1 else 0 end),
[列3中3的个数] = sum(case 列3 when '3' then 1 else 0 end)
from
表
[列1中a的个数] = sum(case 列1 when 'a' then 1 else 0 end),
[列1中b的个数] = sum(case 列1 when 'b' then 1 else 0 end),
[列2中是的个数] = sum(case 列2 when '是' then 1 else 0 end),
[列2中否的个数] = sum(case 列2 when '否' then 1 else 0 end),
[列3中1的个数] = sum(case 列3 when '1' then 1 else 0 end),
[列3中2的个数] = sum(case 列3 when '2' then 1 else 0 end),
[列3中3的个数] = sum(case 列3 when '3' then 1 else 0 end)
from
表
(列1 varchar(2), 列2 varchar(2), 列3 int)
insert into #a
select
'a','是',1 union all select
'a','否',1 union all select
'b','是',2 union all select
'b','是',3select * from #adeclare
@m int,
@n int,
@q int,
@p int,
@r int,
@s int,
@w intset @m=(select count(列1) from #a where 列1='a')
set @n=(select count(列1) from #a where 列1='b')
set @q=(select count(列2) from #a where 列2='是')
set @p=(select count(列2) from #a where 列2='否')
set @r=(select count(列3) from #a where 列3=1)
set @s=(select count(列3) from #a where 列3=2)
set @w=(select count(列3) from #a where 列3=3) select @m as 列1,@n as 列2,@q as 列3,@p as 列4,@r as 列5,@s as 列6,@w as 列7drop table #a