Oracle SQL:查找作业的作业ID,其中最高薪水与最低薪酬之间的差异最高为

时间:2018-02-19 18:38:31

标签: sql oracle

我正在尝试解决一些练习而且我遇到了这个问题,我自己尝试了一些解决方案,但我一直得到错误,例如"错过右括号"

这是我的尝试 select job_id from employees where (select max(max(salary) - min(salary) from employees as job_id)

select jobID from employees where (max(max(salary) - min(salary) as jobID) 在这种情况下,错误是: ORA-00934: group function is not allowed here

此外,我不确定是否应该使用子查询解决此练习。

1 个答案:

答案 0 :(得分:2)

使用Oracle 12.1行限制子句的一种方法(with子句只是在不创建表的情况下提供虚拟数据 - 因为您已经有一个employees表,您可以将其保留并从select job_id):

with employees (emp_id, job_id, salary) as
     ( select 1, 10, 11000 from dual union all
       select 2, 10, 11500 from dual union all
       select 3, 20, 12000 from dual union all
       select 4, 20, 13000 from dual union all
       select 5, 30, 45000 from dual union all
       select 6, 30, 50000 from dual
)
select job_id
     , min(salary), max(salary)
     , max(salary) - min(salary) as salary_range
from   employees
group by job_id
order by salary_range desc
fetch first row only;

    JOB_ID MIN(SALARY) MAX(SALARY) SALARY_RANGE
---------- ----------- ----------- ------------
        30       45000       50000         5000

或者,

with employees (emp_id, job_id, salary) as
     ( select 1, 10, 11000 from dual union all
       select 2, 10, 11500 from dual union all
       select 3, 20, 12000 from dual union all
       select 4, 20, 13000 from dual union all
       select 5, 30, 45000 from dual union all
       select 6, 30, 50000 from dual
     )
select job_id
from   ( select job_id
             , row_number() over (order by max(salary) - min(salary) desc) as seq
         from employees
         group by job_id )
where  seq = 1