原文链接:
    https://www.cnblogs.com/laotan/p/4540677.html

    —根据每个部门来统计部门工资总和

    1. select deptid, sum(sal) 工资合计 from emp group by deptid;


    —根据每个部门来统计部门工资总和

    1. select deptid, 工资合计, sum(工资合计) over() as 总合计
    2. from (select deptid, sum(sal) 工资合计 from emp group by deptid) x;
    1. select
    2. deptid 部门,
    3. 工资合计,
    4. 总合计,
    5. round((工资合计/总合计) * 100 , 2) || '%' as 工资比例
    6. from (select deptid,
    7. 工资合计,
    8. sum(工资合计) over() as 总合计 from (select deptid,
    9. sum(sal) 工资合计
    10. from emp
    11. group by deptid) x ) y
    12. order by 1;

    —round(number,2) 保留下面两位小数 根据截取后一位小数来进行四舍五入
    select round(2342.54665,1) from dual;

    —使用专用的比例函数

    1. select deptid,
    2. 工资合计,
    3. sum(工资合计) over() as 总合计,
    4. round(ratio_to_report(工资合计) over() * 100, 2) || '%' as 工资比例
    5. from (select deptid, sum(sal) 工资合计 from emp group by deptid)
    6. order by 1 desc;

    —使用分析函数 查询每个员工在对应部门中所占的工资比列

    1. select deptid,
    2. ename,
    3. sal,
    4. round(ratio_to_report(sal) over(partition by deptid) * 100, 2) || '%' 工资比例
    5. from emp
    6. order by 1, 2;
    1. SELECT
    2. t.DEPT_NO,
    3. t.DEPT_NAME,
    4. COUNT( * ) AS COUNTS,
    5. sum( COUNT( * ) ) over ( ) AS "total",
    6. round( ratio_to_report ( COUNT( * ) ) over ( ) * 100, 2 ) || '%' AS "proportion"
    7. FROM
    8. T_DEPARTMENT t
    9. GROUP BY
    10. t.DEPT_NO,
    11. t.DEPT_NAME
    12. ORDER BY
    13. t.DEPT_NO
    1. SELECT
    2. a.DEPT_NO AS "deptNo",
    3. a.DEPT_NAME AS "deptName",
    4. a.COUNTS AS "counts",
    5. sum( a.COUNTS ) over ( ) AS "total",
    6. round( ratio_to_report ( a.COUNTS ) over ( ) * 100, 2 ) || '%' AS "proportion"
    7. FROM
    8. (
    9. SELECT
    10. t.DEPT_NO,
    11. t.DEPT_NAME,
    12. COUNT( * ) AS COUNTS
    13. FROM
    14. T_DEPARTMENT t
    15. GROUP BY
    16. t.DEPT_NO,
    17. t.DEPT_NAME
    18. ORDER BY
    19. t.DEPT_NO
    20. ) a