0

我的查询获取每个年级每个部门的员工人数。

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

现在我需要从中获得每个部门的最大数量的成绩。

输出应该是

dept  grades MAX(count)

我怎样才能做到这一点?

谢谢

4

1 回答 1

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
于 2013-03-07T11:37:33.937 回答