2013-04-04 58 views
2

我有2列工作和表emp部門。每個項目明智地計數條件

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)

回答

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

+0

感謝您的查詢,但我需要得到經理(經理+總裁+職員+分析師+安全)的計數14即5 + 1 + 3 + 2 + 3 = 14 – ravt 2013-04-04 13:28:44

+1

@ravt,這就是查詢返回給經理 – 2013-04-04 13:36:49

+0

非常感謝您的寶貴意見 – ravt 2013-04-05 10:57:49