每个项目明智的计数

时间:2013-04-03 12:59:43

标签: 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        status           status_numbers
------------------------------------------------------------------------------
Total               14             Total                                  14
manager             5              manager                                 5
president       1              president                       1
clerk           3              clerk                   3
analyst         2              analyst                 2
security            3              security                3

此处需要重复status和status_numbers列

1 个答案:

答案 0 :(得分:2)

您可以使用rollup执行此操作:

select coalesce(job, 'Total') status, count(*) status_numbers,
       coalesce(job, 'Total') status2, count(*) status_numbers2
  from jobs
 group by rollup(job)
 order by grouping_id(job) desc;

示例:http://sqlfiddle.com/#!4/8d825/1