select 所有人 , sum(语文) as 语文,sum(数学) as 数学,sum(政治) as 政治 from table1 group by 所有人 ----一个普通的分组
语文,数据 政治 都是Varcher型数据,要统计解释为 A交了几次语文 ,几次数据,几次政治
表1 字段名: 登陆人员 分析员 渠道员 销售员 翻译员 字段属性:都是varcher型数据目标例如: 登陆人员 分析员 渠道员 销售员 翻译员 A (分析员=‘A’的个数) (渠道员=‘A’的个数)....... B (分析员=‘B’的个数) (渠道员=‘B’的个数)....... .....
select 登陆人员, count(分析员), count(渠道员),count(销售员),count(翻译员) from tb group by 登陆人员
是步是字段有空的现象. select 登陆人员, sum(case when 分析员 is null then 0 else 1 end) as 分析员, sum(case when 渠道员 is null then 0 else 1 end) as 渠道员, sum(case when 销售员 is null then 0 else 1 end)as 销售员, sum(case when 翻译员 is null then 0 else 1 end) as 翻译员 from tb group by 登陆人员
select 登陆人员, 分析员 = sum(case 分析员 when 登陆人员 then 1 else 0 end), 渠道员 = sum(case 渠道员 when 登陆人员 then 1 else 0 end), 销售员 = sum(case 销售员 when 登陆人员 then 1 else 0 end), 翻译员 = sum(case 翻译员 when 登陆人员 then 1 else 0 end) from 表1 group by 登陆人员 order by 登陆人员
declare @s varchar(8000) set @s = '' select @s = @s + ' union all select 登陆人员='''+登陆人员+'''' + ',分析员 = sum(case 分析员 when '''+登陆人员+''' then 1 else 0 end)' + ',渠道员 = sum(case 渠道员 when '''+登陆人员+''' then 1 else 0 end)' + ',销售员 = sum(case 销售员 when '''+登陆人员+''' then 1 else 0 end)' + ',翻译员 = sum(case 翻译员 when '''+登陆人员+''' then 1 else 0 end)' + ' from 表1' from (select distinct 登陆人员 from 表1) aset @s = stuff(@s,1,10,'') exec(@s)
from table1 group by 所有人
----一个普通的分组
字段名: 登陆人员 分析员 渠道员 销售员 翻译员
字段属性:都是varcher型数据目标例如:
登陆人员 分析员 渠道员 销售员 翻译员
A (分析员=‘A’的个数) (渠道员=‘A’的个数).......
B (分析员=‘B’的个数) (渠道员=‘B’的个数).......
.....
select 登陆人员, sum(case when 分析员 is null then 0 else 1 end) as 分析员,
sum(case when 渠道员 is null then 0 else 1 end) as 渠道员,
sum(case when 销售员 is null then 0 else 1 end)as 销售员,
sum(case when 翻译员 is null then 0 else 1 end) as 翻译员
from tb group by 登陆人员
登陆人员,
分析员 = sum(case 分析员 when 登陆人员 then 1 else 0 end),
渠道员 = sum(case 渠道员 when 登陆人员 then 1 else 0 end),
销售员 = sum(case 销售员 when 登陆人员 then 1 else 0 end),
翻译员 = sum(case 翻译员 when 登陆人员 then 1 else 0 end)
from
表1
group by
登陆人员
order by
登陆人员
set @s = ''
select @s = @s + ' union all select 登陆人员='''+登陆人员+''''
+ ',分析员 = sum(case 分析员 when '''+登陆人员+''' then 1 else 0 end)'
+ ',渠道员 = sum(case 渠道员 when '''+登陆人员+''' then 1 else 0 end)'
+ ',销售员 = sum(case 销售员 when '''+登陆人员+''' then 1 else 0 end)'
+ ',翻译员 = sum(case 翻译员 when '''+登陆人员+''' then 1 else 0 end)'
+ ' from 表1'
from
(select distinct 登陆人员 from 表1) aset @s = stuff(@s,1,10,'')
exec(@s)