2013-03-07 82 views
0

我的查詢得到每個等級每個部門的員工數。按部門劃分後分組得分的最大值

select dept , grade , count(1) CNT 
from mytable 
group by dept , grade 
order by dept , cnt desc; 

現在我需要從每個部門的最大數量得分。

輸出應該

dept grades MAX(count) 

我該怎麼辦呢?

感謝

+2

您正在使用什麼RDBMS? (MySQL和甲骨文,SQL的服務器,...)? – 2013-03-07 11:36:53

回答

0

要做到這一點,最好的辦法是使用row_number()功能:

select dept, grade, cnt 
from (select dept, grade, count(*) as cnt, 
      row_number() over (partition by dept order by count(*) desc) as seqnum 
     from mytable t 
     group by dept, grade 
    ) t 
where seqnum = 1