这个是在一条语句中统计的啊: select 级别,sum(1) as 个数 from 表 group by 级别 --是否是需要横向显示啊,用: select [A]=sum(case 级别 when 'A' then 1 end) ,[B]=sum(case 级别 when 'B' then 1 end) ,[C]=sum(case 级别 when 'C' then 1 end) from 表
/*--下面是数据测试,看那个是你需要的--*/--创建数据测试环境 declare @tb table(级别 varchar(1)) insert into @tb select 'A' union all select 'B' union all select 'B' union all select 'C' union all select 'B' union all select 'A' union all select 'C'--纵向显示方式 select 级别,sum(1) as 个数 from @tb group by 级别--横向显示方式: select [A]=sum(case 级别 when 'A' then 1 end) ,[B]=sum(case 级别 when 'B' then 1 end) ,[C]=sum(case 级别 when 'C' then 1 end) from @tb
测试结果:1.纵向的结果 级别 个数 ---- ----------- A 2 B 3 C 2 2.横向的结果 A B C ----------- ----------- ----------- 2 3 2
假设有表table里有“级别”字段 “级别”分三级。分别是 。A ,B ,C。
我想统计A 的个数。B的个数。C的个数。
不是分别统计。而是再一条语句中分别统计出A ,B,C的值。有没有办法。
select 级别,sum(1) as 个数 from 表 group by 级别
--是否是需要横向显示啊,用:
select
[A]=sum(case 级别 when 'A' then 1 end)
,[B]=sum(case 级别 when 'B' then 1 end)
,[C]=sum(case 级别 when 'C' then 1 end)
from 表
declare @tb table(级别 varchar(1))
insert into @tb
select 'A'
union all select 'B'
union all select 'B'
union all select 'C'
union all select 'B'
union all select 'A'
union all select 'C'--纵向显示方式
select 级别,sum(1) as 个数 from @tb group by 级别--横向显示方式:
select
[A]=sum(case 级别 when 'A' then 1 end)
,[B]=sum(case 级别 when 'B' then 1 end)
,[C]=sum(case 级别 when 'C' then 1 end)
from @tb
级别 个数
---- -----------
A 2
B 3
C 2
2.横向的结果
A B C
----------- ----------- -----------
2 3 2