2017-04-18 84 views

回答

0

如果您提供了表格結構,那應該會更好。

但這給一試:

SELECT deptname 
FROM dept 
WHERE deptid = (SELECT distinct deptid 
       FROM employee 
       ORDER BY COUNT(dept) limit 1); 
+0

錯誤在命令行:5列:24 錯誤報告: SQL錯誤:ORA-00907:缺少右括號 00907 。00000 - 「缺少右括號」 *原因: *操作: 括號看行不行得我:/ –

0

我只是回答,因爲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; 
+0

^這個查詢顯示在他們的員工人數所有部門。我想列出一個員工最少的 –

+0

@AhsonJunani。 。 。查詢沒有顯示所有部門。他們展示了一個部門,但恰巧是員工人數最多的部門。使用'asc'而不是'desc'獲得最少的員工。 –

0

請嘗試以下操作...

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); 

該語句開始與內部查詢接合deptemployee上的共享字段deptid。然後按部門對結果行進行分組,並返回外部查詢部門的idname以及該部門的員工數。

外查詢組由部門數據再次,然後選擇具有員工等於員工的最小計數的計數部(多個)的細節。

如果您有任何問題或意見,請隨時發佈相應評論。

+0

錯誤在命令行:12列:3 錯誤報告: SQL錯誤:ORA-00933:SQL命令不能正確地結束 00933. 00000 - 「SQL命令不能正確地結束」 *原因: *操作: –

+0

擺脫'AS employeeCounter'。 – toonice

+0

錯誤在命令行:8列:19 錯誤報告: SQL錯誤:ORA-00918:列定義的含糊 00918. 00000 - 「定義的含糊列」 *原因: *操作: –

0

嗨:)所以我的回答有點難看,並且充滿了嵌套查詢。但是我測試了它和它的工作對我來說...

-- 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; 

這個查詢給出了正確的答案對我來說,甚至當我更改的記錄,使資金的正確答案。 如果您有任何問題,給我喊過意見:)

0
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; 
0
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; 
相關問題