TommyLotus

导航

Sql server 分组统计样例

 

select ROW_NUMBER()OVER(ORDER BY 模块分组) as 序号 ,模块分组,count(0)as 总表数,
sum(case 导换标志 when 'Y' then 1 else 0 endas 需要迁移,
sum(case 导换标志 when 'N' then 1 else 0 endas 不需要迁移,
sum(case 导换标志 when '?' then 1 else 0 endas 未确定,
sum(case 是否已导 when N'' then 1 else 0 endas 已经导换,
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 endas 六级总数,
sum(case when 重要级别='6' and 是否已导=N'' then 1 else 0 endas 六级已导,
sum(case 重要级别 when '5' then 1 else 0 endas 五级总数,
sum(case when 重要级别='5' and 是否已导=N'' then 1 else 0 endas 五级已导,
sum(case 重要级别 when '4' then 1 else 0 endas 四级总数,
sum(case when 重要级别='4' and 是否已导=N'' then 1 else 0 endas 四级已导
from 样例表  group by 模块分组 
order by 模块分组

posted on 2009-03-12 21:23  Tommy.net  阅读(794)  评论(0编辑  收藏  举报