我有这些表格:
部
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
我做错了什么?
答案 0 :(得分:1)
不确定我理解你的问题,但我想你想要这个:
algebraixlib
返回:
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;
SQLFiddle示例:http://sqlfiddle.com/#!4/05db83/1
答案 1 :(得分:0)
根据您的示例数据,该城市似乎记录在address
表的department
列中,并且多个部门可以拥有相同的城市。
考虑到这一点,以及你想要“排除达拉斯”的事实,正如你所说,我认为你想要过滤掉相关address
列是'达拉斯'的所有部门。
然而,我不知道是否......
如果(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)));
答案 2 :(得分:0)
试试这个。
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))
答案 3 :(得分:0)
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);
希望有所帮助