获取员工人数最多的部门名称

时间:2019-06-10 11:29:12

标签: sql oracle

我有2个表,一个表存储人员的详细信息(列为staff_id,staff_name,department_id),另一个表存储部门的详细信息(列为department_id,department_name,department_block_num)。

我需要写一个查询来显示按部门名称排序最多的部门名称的部门名称。还假定多个部门也可以拥有与最大员工数相同的员工数。另一个条件是不允许使用组功能。

这是代码

SELECT department_name
  FROM department
 WHERE department_id IN (SELECT department_id
                           FROM (  SELECT department_id, COUNT (*) AS cnt1
                                     FROM staff
                                    WHERE COUNT (*) =
                                             (SELECT cnt
                                                FROM (  SELECT department_id,
                                                               COUNT (*) AS cnt
                                                          FROM staff
                                                      GROUP BY department_id
                                                      ORDER BY cnt DESC)
                                               WHERE ROWNUM = 1)
                                 GROUP BY department_id));

3 个答案:

答案 0 :(得分:0)

如果您尝试避免使用group by,请使用子查询:

select d.*,
       (select count(*)
        from staff s
        where s.department_id = d.department_id
       ) as staff_cnt
from department d;

如果您想让高层部门保持联系,请使用子查询和窗口函数:

select . . .   -- whatever columns you want
from (select d.*,
           rank() over (order by staff_cnt desc) as seqnum
      from (select d.*,
                   (select count(*)
                    from staff s
                    where s.department_id = d.department_id
                   ) as staff_cnt
            from department d
           ) d
      ) d
where seqnum = 1;

答案 1 :(得分:0)

您可以使用RANK分析函数来查找具有最大计数的行:

SELECT   department_id,
         COUNT(*) AS cnt,
         RANK() OVER ( ORDER BY COUNT(*) DESC ) AS rnk
FROM     staff
GROUP BY department_id

将按DESC行结束对职员进行排名,然后您可以对排名为1(且具有最高人数)的行进行过滤。

Oracle设置

CREATE SEQUENCE staff__id__seq;

CREATE TABLE departments (
  id              INT PRIMARY KEY,
  department_name VARCHAR2(20)
);

INSERT INTO departments ( id, department_name )
  SELECT 1, 'Aaa' FROM DUAL UNION ALL
  SELECT 2, 'Bbb' FROM DUAL UNION ALL
  SELECT 3, 'Ccc' FROM DUAL UNION ALL
  SELECT 4, 'Ddd' FROM DUAL;

CREATE TABLE staff (
  id INT PRIMARY KEY,
  department_id INT REFERENCES departments( id )
);

INSERT INTO staff ( id, department_id )
SELECT staff__id__seq.NEXTVAL, department_id
FROM (
  SELECT 1 AS department_id FROM DUAL CONNECT BY LEVEL <= 3 UNION ALL
  SELECT 2                  FROM DUAL CONNECT BY LEVEL <= 5 UNION ALL
  SELECT 3                  FROM DUAL CONNECT BY LEVEL <= 2 UNION ALL
  SELECT 4                  FROM DUAL CONNECT BY LEVEL <= 5
);

查询人员计数

SELECT   department_id,
         COUNT(*) AS cnt,
         RANK() OVER ( ORDER BY COUNT(*) DESC ) AS rnk
FROM     staff
GROUP BY department_id
DEPARTMENT_ID | CNT | RNK
------------: | --: | --:
            2 |   5 |   1
            4 |   5 |   1
            1 |   3 |   3
            3 |   2 |   4

查询2-获取最高计数的部门名称

只需将上一个查询添加到departments表中,然后过滤即可在排名为1(最高计数)时返回行。

SELECT d.department_name
FROM   (
         SELECT   department_id,
                  RANK() OVER ( ORDER BY COUNT(*) DESC ) AS rnk
         FROM     staff
         GROUP BY department_id
       ) s
       INNER JOIN departments d
       ON ( d.id = s.department_id )
WHERE  s.rnk = 1

输出

| DEPARTMENT_NAME |
| :-------------- |
| Bbb             |
| Ddd             |

db <>提琴here

答案 2 :(得分:0)

假设这是家庭作业或类似作业,并且不允许使用Windows函数,这是使用更基本的sql的解决方案

SELECT department_name
FROM department d
JOIN staff s ON d.department_id = s.department_id
GROUP BY department_name
HAVING COUNT(s.department_id) = (SELECT COUNT(*) as stat 
    FROM staff 
    GROUP BY department_id 
    ORDER BY stat DESC
    FETCH FIRST ROW ONLY)
ORDER BY department_name