【问题标题】:Department has Maximum Staff部门有最大的工作人员
【发布时间】:2020-05-29 14:38:49
【问题描述】:

编写查询以按部门名称显示具有最大员工数量顺序的部门名称。

这是我的问题的架构:

我试过了:

select d.department_name
from   department d
       inner join staff s on d.department_id=s.department_id
group by d.department_name
having count(s.staff_id) >= all
       ( select count(s.staff_id) as cnt
         from   department d
                inner join staff s on d.department_id=s.department_id
         group by department_name )
order by d.department_name desc;

我能够通过一个导致“SE”部门的测试用例,但我无法通过另一个测试用例。我不知道第二个测试用例想要什么。我不确定我在上面的代码中做错了什么。

【问题讨论】:

  • 您能否包含创建架构、填充测试数据、测试用例的细节以及测试结果所需的命令?

标签: sql oracle join


【解决方案1】:

您可以按如下方式使用windows功能:

select department_name, cnt
from
(select department_name, rank() over (order by cnt desc) as rn, cnt 
from
(select d.department_name, count(1) cnt
   from department d inner join staff s 
     on d.department_id=s.department_id
      group by d.department_name))
where rn = 1
order by department_name

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-05-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-20
    • 2012-08-02
    • 1970-01-01
    相关资源
    最近更新 更多