oracle中如何求百分比?

求将第一个表中的数据,按照第二个表中的格式显示出来。小弟只会做平均年龄和各组人数,不会求百分比啊!求大神赐教!
select wh ,avg(age) , count(wh) from employee
group by wh

实现代码:

ELECT(CASE WHEN db_psndoc.age<=30 THEN '30岁以上'

WHEN db_psndoc.age>30 THEN '30岁及以下' END)

ranges, COUNT(*) rs ,100*round(COUNT(*)/SUM(COUNT(*))

OVER(),4)||'%' percent FROM bd_psnd

GROUP BY CASE

WHEN bd_psndoc.age<=30 then '30岁及以下'

WHEN db_psndoc.age<=30 THEN '30岁以上'

END

扩展资料

sum(..) over(..)用法分析:

sum(…) over( ),对所有行求和;

sum(…) over( order by … ), 连续求和;

sum(…) over( partition by… ),同组内所行求和;

sum(…) over( partition by… order by … ),同第1点中的排序求和原理,只是范围限制在组内。

over不能单独使用,要和分析函数:rank(),dense_rank(),row_number(),sum()等一起使用。

over函数的参数:over(partition by columnname1 order by columnname2)

含义,按columname1指定的字段进行分组排序,或者说按字段columnname1的值进行分组排序。

例子:

select deptno,ename,sal,

sum(sal) over (partition by deptno order by ename) 部门连续求和,--各部门的薪水"连续"求和

sum(sal) over (partition by deptno) 部门总和, -- 部门统计的总和,同一部门总和不变

温馨提示:答案为网友推荐,仅供参考
第1个回答  推荐于2017-09-14
select a.wh,a.age,a.count1,round((a.count1/b.count2),2)*100||'%'
from
(select wh ,avg(age) age , count(wh) count1 from employee group by wh) a,
(select count(*) count2 from employee) b

这样试试

本回答被提问者采纳
第2个回答  2013-12-07
select wh ,avg(age),count(wh),
    count(wh)/(select count(*) from employee)*100 as 百分比
from employee
group by wh

追问

先收到了第一个回答,所以就采纳为满意答案了,看了您的答案觉得更专业点了!谢谢大神了!

相似回答