回答
如果您提供了表格結構,那應該會更好。
但這給一試:
SELECT deptname
FROM dept
WHERE deptid = (SELECT distinct deptid
FROM employee
ORDER BY COUNT(dept) limit 1);
我只是回答,因爲MIN
不需要和LIMIT
是不是Oracle:
select d.*
from (select deptid, count(*) as cnt
from employees e
group by deptid
order by count(*) asc
) d
where rownum = 1;
在Oracle 12C +,你不需要子查詢:
select deptid, count(*) as cnt
from employees e
group by deptid
order by count(*) asc
fetch first 1 row only;
^這個查詢顯示在他們的員工人數所有部門。我想列出一個員工最少的 –
@AhsonJunani。 。 。查詢沒有顯示所有部門。他們展示了一個部門,但恰巧是員工人數最多的部門。使用'asc'而不是'desc'獲得最少的員工。 –
請嘗試以下操作...
SELECT deptid,
deptname,
employeeCount
FROM
(
SELECT dept.deptid AS deptid,
dept.deptname,
COUNT(dept.deptid) AS employeeCount
FROM dept
JOIN employee ON dept.deptid = employee.deptid
GROUP BY dept.deptid,
dept.deptname
)
GROUP BY deptid
HAVING employeeCount = MIN(employeeCount);
該語句開始與內部查詢接合dept
到employee
上的共享字段deptid
。然後按部門對結果行進行分組,並返回外部查詢部門的id
和name
以及該部門的員工數。
外查詢組由部門數據再次,然後選擇具有員工等於員工的最小計數的計數部(多個)的細節。
如果您有任何問題或意見,請隨時發佈相應評論。
錯誤在命令行:12列:3 錯誤報告: SQL錯誤:ORA-00933:SQL命令不能正確地結束 00933. 00000 - 「SQL命令不能正確地結束」 *原因: *操作: –
擺脫'AS employeeCounter'。 – toonice
錯誤在命令行:8列:19 錯誤報告: SQL錯誤:ORA-00918:列定義的含糊 00918. 00000 - 「定義的含糊列」 *原因: *操作: –
嗨:)所以我的回答有點難看,並且充滿了嵌套查詢。但是我測試了它和它的工作對我來說...
-- First I created a couple of test tables and added a few records
drop table dept;
drop table employee;
create table dept (deptid number primary key, deptname varchar(20));
create table employee(employee_id number primary key, names varchar(20),
deptid number,foreign key (deptid) references dept(deptid));
insert into dept values(1,'HR');
insert into dept values(2,'Finance');
insert into dept values(3,'IT');
insert into employee values(1,'Tina',1);
insert into employee values(2,'Rob',1);
insert into employee values(3,'Lisa',1);
insert into employee values(4,'Will',2);
insert into employee values(5,'Lina',2);
insert into employee values(6,'Ethel',2);
insert into employee values(7,'Trevor',1);
insert into employee values(8,'Alanea',1);
insert into employee values(9,'Matthew',1);
insert into employee values(10,'Maddie',3);
insert into employee values(11,'Anna',1);
-- According to the added records, the answer we are looking for should be
the department name IT
-- select the department name from department table
select d.deptname from dept d,
/* This is where it gets ugly - basically, it counts the number of
employees in each department, then finds the id of the department that had
the smallest count */
(select deptid from
(select count(deptid) as counter, deptid from employee group by deptid)
where counter =(select min(counter)from
(select count(deptid) as counter, deptid from employee group by deptid))) minid
-- join the tables using deptid
where d.deptid = minid.deptid;
這個查詢給出了正確的答案對我來說,甚至當我更改的記錄,使資金的正確答案。 如果您有任何問題,給我喊過意見:)
select department_name, count(employee_id)
from department d
inner join employee e
on d.employee_id = e.employee_id
having count(employee_id) =
(
select min(count(employee_id)) /*This query returns minimum count*/
from department d
inner join employee e
on d.employee_id = e.employee_id
group by department_name
)
group by department_name;
WITH temp
AS
(SELECT e1.department_id, count(e1.employee_id) emp_count
FROM hr.employees e1
GROUP BY e1.department_id)
SELECT d1.department_name, t1.emp_count employee_count
FROM temp t1
,hr.departments d1
WHERE t1.department_id = d1.department_id(+)
AND NOT EXISTS
(SELECT 1
FROM temp t2
WHERE t2.emp_count < t1.emp_count)
ORDER BY 2,1;
- 1. 返回每個員工部門名稱
- 2. 如何列出部門編號和名稱,然後列出在部門名稱下的部門中工作的員工?
- 3. 列出所有員工在某個項目上工作的部門名稱
- 4. SQL查詢,顯示部門名稱,地點名稱,員工
- 5. 填寫詢問顯示部門名稱,職位名稱,「各部門職員的人數」和「部門職員數」
- 6. 列出每個部門的前十名員工
- 7. SQL-HR架構,檢索部門名稱,每個部門的經理和員工
- 8. 列出員工姓名與自己當前的工作/部門(表職員LEFT JOIN表Department_Staff)
- 9. 按員工數量降序排列部門名稱的SQL查詢
- 10. 員工,部門和單位
- 11. 員工和部門和Sql
- 12. 查找部門有員工
- 13. sql,員工和部門
- 14. 列出每個部門中最近僱傭的員工
- 15. 顯示員工姓名,部門名稱,薪水,年級,經驗(EX:XX年YY月ZZ天)SQL的所有員工
- 16. 顯示每個部門的編號和名稱,並在各部門採用的員工數量
- 17. 前兩名員工每個部門的工資差額
- 18. SQL - 選擇名稱和其員工的最大數量各部門的地址
- 19. 根據層次樹將部門員工關聯到部門
- 20. 集團的員工,每個部門
- 21. 員工,部門和ManagerID表結構
- 22. 顯示no。使用匿名塊的部門員工數
- 23. 檢索員工誰在他們的部門至少使名字
- 24. 如何在選擇部門時獲得員工名單?
- 25. 不同的部門名稱 - Sharepoint搜索
- 26. 部門名稱和學生人數
- 27. 選擇部門名稱預查詢requiste
- 28. 員工和部門與羣體數據庫架構諮詢
- 29. 獲得品牌名稱和部門名稱
- 30. PL/SQL:遊標::檢索員工列表每個部門
錯誤在命令行:5列:24 錯誤報告: SQL錯誤:ORA-00907:缺少右括號 00907 。00000 - 「缺少右括號」 *原因: *操作: 括號看行不行得我:/ –