我不能在Oracle中使用min和max

时间:2019-08-29 15:18:20

标签: sql oracle oracle11g

我想找到数量较少的工作,但是我不能在hading子句上使用min函数。

SELECT JOB, COUNT(*) AS "NUMBER OF EMPLOYEES"
FROM EMP
GROUP BY JOB
HAVING COUNT(*) = MIN(COUNT(*))
ORDER BY JOB

2 个答案:

答案 0 :(得分:1)

您可以像这样使用FETCH子句

SELECT JOB, COUNT(*) AS "NUMBER OF EMPLOYEES"
FROM EMP
GROUP BY JOB
ORDER BY "NUMBER OF EMPLOYEES"
FETCH FIRST ROW ONLY

哦!其oracle 11g,然后使用以下命令:

SELECT JOB, COUNT(*) AS "NUMBER OF EMPLOYEES"
FROM EMP
GROUP BY JOB
HAVING COUNT(*) = (SELECT MIN(COUNT(*)
FROM EMP GROUP BY JOB)
ORDER BY JOB

干杯!

答案 1 :(得分:1)

您要计算每个职位的员工数。然后,您想要显示员工人数最少的工作。这是两个步骤。

一种方法:

select job, employees
from
(
  select job, count(*) as employees, min(count(*)) over () as min_employees
  from emp
  group by job
)
where employees = min_employees;

另一个:

with jobs as (select job, count(*) as employees from emp group by job)
select *
from jobs
where employees = (select min(employees) from jobs);