在dept和grade上逐个组后获得每个部门的最大计数值

时间:2013-03-07 11:34:43

标签: sql

我的查询得到每个年级的每个部门的员工数。

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

现在我需要获得每个部门的最高分数。

输出应该是

dept  grades MAX(count)

我该怎么做?

由于

1 个答案:

答案 0 :(得分: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