繁体   English   中英

SQL-选择雇员人数最多的所有部门的名称和地址

[英]SQL - select name and address of all departments having max number of employees

我有这些表:

DEPARTMENT_ID DEPARTMENT_NAME      ADDRESS            
------------- -------------------- --------------------
       10 ACCOUNTING           NEW YORK            
       20 RESEARCH             DALLAS              
       30 SALES                CHICAGO             
       40 IT                   DALLAS              
       50 EXECUTIVE            NEW YORK            
       60 MARKETING            CHICAGO     

雇员

Employee_ID  employee_name  job                     manager_ID  hire_date  salary  commission  department_ID
------------------------------------------------------------------------------------------------------------
7839        KING            PRESIDENT                           20-NOV-01   5000                50
7596        JOST            VICE PRESIDENT          7839        04-MAY-01   4500                50
7603        CLARK           VICE PRESIDENT          7839        12-JUN-01   4000                50
7566        JONES           PUBLIC ACCOUNTANT       7596        05-APR-01   3000                10
7886        STEEL           PUBLIC ACCOUNTANT       7566        08-MAR-03   2500                10
7610        WILSON          ANALYST                 7596        03-DEC-01   3000                20
7999        WOLFE           ANALYST                 7610        15-FEB-02   2500                20
7944        LEE             ANALYST                 7610        04-SEP-06   2400                20
7900        FISHER          SALESMAN                7603        06-DEC-01   3000    500         30
7921        JACKSON         SALESMAN                7900        25-FEB-05   2500    400         30
7952        LANCASTER       SALESMAN                7900        06-DEC-06   2000    150         30
7910        SMITH           DATABASE ADMINISTRATOR  7596        20-DEC-01   2900                40
7788        SCOTT           PROGRAMMER              7910        15-JAN-03   2500                40
7876        ADAMS           PROGRAMMER              7910        15-JAN-03   2000                40
7934        MILLER          PROGRAMMER              7876        25-JAN-02   1000                40
8000        BREWSTER        TBA                                 22-AUG-13   2500    

我需要显示姓名和地址(达拉斯除外),雇员人数最多。

我写了这个:

SELECT department_name, address
FROM department
WHERE department_id IN
                 (SELECT   MAX(department_id)
                  FROM     department
                  WHERE    UPPER(address) != 'DALLAS')
ORDER BY department_name; 

但我只得到一行

DEPARTMENT_NAME      ADDRESS            
-------------------- --------------------
MARKETING            CHICAGO             

我究竟做错了什么?

不确定我是否理解您的问题,但我认为您想要这样做:

with emp_count as (
  select d.department_name,
         d.address,
         count(*) as num_emps, 
         max(count(*)) over () as max_count
  from department d 
    join employee e on d.department_id = e.department_id
  where address <> 'DALLAS'
  group by d.department_name, d.address
)
select * 
from emp_count
where num_emps = max_count;

返回:

DEPARTMENT_NAME | ADDRESS  | NUM_EMPS
----------------+----------+---------
SALES           | CHICAGO  |        3
EXECUTIVE       | NEW YORK |        3

SQLFiddle示例: http ://sqlfiddle.com/#!4/05db83/1

根据您的样本数据,该城市似乎记录在department表的address栏中,并且一个以上的部门可以拥有同一城市。

考虑到这一点,以及您想要“排除达拉斯”的事实,我假设您要过滤掉关联address列为“达拉斯”的所有部门。

但是,我不知道是否...

  1. 如果您想将达拉斯的部门与其他部门联系最多的部门,则将其排除在外,或者
  2. 如果要在确定任何部门的最高员工人数时不考虑达拉斯的部门。

如果(1)为真:

select *
  from department_tbl
 where department_id in (select department_id
                           from employee_tbl
                          group by department_id
                         having count(*) = (select max(num_emps)
                                             from (select department_id,
                                                          count(*) as num_emps
                                                     from employee_tbl
                                                    group by department_id)))
   and address <> 'DALLAS';

如果(2)为真:

select *
  from department_tbl
 where department_id in (select department_id
                           from employee_tbl
                          group by department_id
                         having count(*) = (select max(num_emps)
                                             from (select department_id,
                                                          count(*) as num_emps
                                                     from employee_tbl
                                                    where address <> 'DALLAS'
                                                    group by department_id)));

尝试这个。

SELECT d.department_name, 
       d.address 
FROM   department d 
       JOIN employee e 
         ON ( d.department_id = e.department_id ) 
WHERE  d.address <> 'DALLAS' 
GROUP  BY d.department_name, 
          d.address 
HAVING Count(*) = (SELECT Max(cnt) 
                   FROM   (SELECT Count(*) CNT 
                           FROM   employee e 
                           WHERE  NOT EXISTS (SELECT 'x' 
                                              FROM   department d 
                                              WHERE  d.department_id = 
                                                     e.department_id 
                                                     AND d.address = 'DALLAS') 
                           GROUP  BY department_id)) 
select e.department_id, d.department_name, count(e.department_id)
from employee e, department d
where e.department_id = d.department_id
group by e.department_id, d.department_name 
having count(e.department_id)=(select max(count(department_id)) 
                               from employee 
                               group by department_id);

希望能帮助到你

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM