SQL除以条款中的问题

时间:2012-07-25 05:12:51

标签: sql oracle

当我尝试这样做时

select first_name from employees 
group by salary,first_name
having salary between 0 and 4800

我正在获得49条记录

当我尝试这样做时,我没有得到任何结果

select first_name from employees 
group by salary,first_name
having salary between 0 and (max(salary)/5)

此处select max(salary)/5 from employees fetches:4800

这很简单,但我不知道我在做什么愚蠢的错误

3 个答案:

答案 0 :(得分:2)

在本声明中

select first_name from employees 
group by salary,first_name
having salary between 0 and (max(salary)/5)

你按薪水和first_name分组,这可能只分组一行。你基本上是在寻找介于0和X / 5之间的X,这可能不会产生任何结果。

在本声明中

select max(salary)/5 from employees fetches

你是无所事事的分组,所以你得到每个人的最高工资。

我想这个

select first_name from employees 
WHERE salary between 0 and (SELECT max(salary) FROM employees)/5

是你要找的东西

答案 1 :(得分:0)

您不能在where子句中使用聚合函数。

使用此:

select first_name from employees 
group by salary,first_name
having salary between 0 and (SELECT max(salary)/5 from employees)

答案 2 :(得分:0)

我会使用子查询来避免第二次传递数据:

select first_name
from (select first_name
            ,salary
            ,max(salary) over () max_salary
      from employees)
where salary between 0 and max_salary/5
group by salary,first_name;

虽然我不知道你为什么按薪水和名字分组。