2013-04-03 82 views
-1

我有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  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_numbers列需要重複

回答

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

+0

非常感謝您................ – ravt

+0

如何在上面的查詢中加入deptno以加入dept表? – ravt

+0

@ravt只需將它添加到選擇+組。例如http://sqlfiddle.com/#!4/77780/1。如果在那裏顯示兩個選項,一個在部門+工作上有一個總計,另一個沒有,因爲你沒有說出你想要輸出的樣子。 – DazzaL