这是一个基于上一篇文章的单独问题:
[Link] How to calculate Grand Totals when using OVER (partition by ...) using Oracle SQL
我正在使用以下Oracle SQL查询:
SELECT JOB_GROUP,
SUM(PEOPLE) AS CURRENT_PEOPLE
SUM(PEOPLE) / '100' AS RATIO
FROM MY_VIEW
WHERE MONTH = '01-DEC-2013'
GROUP BY ROLLUP (OB_GROUP)
ORDER BY RATIO DESC;
此查询为我提供了这样的输出..
JOB_GROUP CURRENT_PEOPLE RATIO
WORKER1 100 1.0
WORKER2 30 .3
WORKER3 40 .4
WORKER4 25 .25
WORKER5 30 .30
(NULL) 225 2.25
我需要能够将所有比率的总和除以job_group ='WORKER1',WORKER2,WORKER4的比率之和。例如,我们可以有5个不同的job_groups,但我只需要1,2和4的比率之和。我怀疑这是某种类型的子查询,但还没有得到正确的结果。
该等式看起来像工人1,2,4的总和(比率)/总和(比率)。 2.25 /(1.0 + .3 + .25)= .80
感谢您提供的任何帮助,
答案 0 :(得分:0)
以下使用分析函数来获取worker1的数量:
select JOB_GROUP, CURRENT_PEOPLE,
CURRENT_PEOPLE / sum(job_group = 'WORKER1' then CURRENT_PEOPLE end) over () as Ratio
from (SELECT JOB_GROUP,
SUM(PEOPLE) AS CURRENT_PEOPLE
FROM my_view
WHERE MONTH = '01-DEC-2013'
GROUP BY ROLLUP (JOB_GROUP)
) t
ORDER BY RATIO DESC;