每个项目明智地计算条件

时间:2013-04-04 12:24:03

标签: oracle

表emp中有2列作业和部门。

job                   department
-------------------------------------------
manager              accounting
president            accounting
manager              accounting
clerk                accounting
manager              accounting
manager              accounting
manager              accounting
clerk                accounting
analyst              accounting
security             accounting
security             accounting
clerk                accounting
analyst              accounting
security             accounting

我需要提取如下数据

status       status_numbers        status2           status_numbers2            outstanding                     total
-----------------------------------------------------------------------------------------------------------------
manager             5              manager               14                 total-status_numbers2               14
president           1              president             9                  total-status_numbers2               14 
clerk               3              clerk                 8                  total-status_numbers2               14
analyst             2              analyst               5                  total-status_numbers2               14
security            3              security              3                  total-status_numbers2               14

在status_numbers2列中,我们需要以下面的方式提取数据 经理 o progress_cmc_status_numbers字段中经理,总裁,文员,分析师,安全人员的总和(14)

总统  来自progress_cmc_status_numbers字段的总裁,职员,分析师,安全总和(9)

为职员 来自progress_cmc_status_numbers字段的职员,分析师,安全性总和(8)

分析师 分析人员的总和,来自progress_cmc_status_numbers字段的安全性(5)

为了安全 来自progress_cmc_status_numbers字段(3)的安全性总和

1 个答案:

答案 0 :(得分:4)

尝试这样的事情:

select status ,status_numbers, SUM(status_numbers) OVER (ORDER BY ordr desc ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) status_numbers2
from
(
select "job" status, count(*) status_numbers , decode("job", 'manager', 1, 'president',2, 'clerk', 3,'analyst', 4, 'security', 5) ordr
from emp
group by "job") t

Here is a sqlfiddle