Sql server 分组统计样例
select ROW_NUMBER()OVER(ORDER BY 模块分组) as 序号 ,模块分组,count(0)as 总表数,
sum(case 导换标志 when 'Y' then 1 else 0 end) as 需要迁移,
sum(case 导换标志 when 'N' then 1 else 0 end) as 不需要迁移,
sum(case 导换标志 when '?' then 1 else 0 end) as 未确定,
sum(case 是否已导 when N'是' then 1 else 0 end) as 已经导换,
convert(varchar(8),convert(decimal(6,2),100*sum(case 是否已导 when N'是' then 1.0 else 0.0 end)
/sum(case 导换标志 when 'Y' then 1.0 else 0.0 end)))+'%' as 导换比例,
convert(varchar(8),convert(decimal(6,2),100*(sum(case 是否已导 when N'是' then 1.0 else 0.0 end)+sum(case 导换标志 when 'N' then 1.0 else 0.0 end))
/count(0)))+'%' as 完成比例,
sum(case 重要级别 when '6' then 1 else 0 end) as 六级总数,
sum(case when 重要级别='6' and 是否已导=N'是' then 1 else 0 end) as 六级已导,
sum(case 重要级别 when '5' then 1 else 0 end) as 五级总数,
sum(case when 重要级别='5' and 是否已导=N'是' then 1 else 0 end) as 五级已导,
sum(case 重要级别 when '4' then 1 else 0 end) as 四级总数,
sum(case when 重要级别='4' and 是否已导=N'是' then 1 else 0 end) as 四级已导
from 样例表 group by 模块分组
order by 模块分组
sum(case 导换标志 when 'Y' then 1 else 0 end) as 需要迁移,
sum(case 导换标志 when 'N' then 1 else 0 end) as 不需要迁移,
sum(case 导换标志 when '?' then 1 else 0 end) as 未确定,
sum(case 是否已导 when N'是' then 1 else 0 end) as 已经导换,
convert(varchar(8),convert(decimal(6,2),100*sum(case 是否已导 when N'是' then 1.0 else 0.0 end)
/sum(case 导换标志 when 'Y' then 1.0 else 0.0 end)))+'%' as 导换比例,
convert(varchar(8),convert(decimal(6,2),100*(sum(case 是否已导 when N'是' then 1.0 else 0.0 end)+sum(case 导换标志 when 'N' then 1.0 else 0.0 end))
/count(0)))+'%' as 完成比例,
sum(case 重要级别 when '6' then 1 else 0 end) as 六级总数,
sum(case when 重要级别='6' and 是否已导=N'是' then 1 else 0 end) as 六级已导,
sum(case 重要级别 when '5' then 1 else 0 end) as 五级总数,
sum(case when 重要级别='5' and 是否已导=N'是' then 1 else 0 end) as 五级已导,
sum(case 重要级别 when '4' then 1 else 0 end) as 四级总数,
sum(case when 重要级别='4' and 是否已导=N'是' then 1 else 0 end) as 四级已导
from 样例表 group by 模块分组
order by 模块分组