0

当我尝试这样做时

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

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

4

3 回答 3

2

在这份声明中

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

您按薪水和名字分组,这很可能只按一行分组。您实际上是在寻找介于 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

是你要找的

于 2012-07-25T05:17:19.053 回答
0

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

用这个:

select first_name from employees 
group by salary,first_name
having salary between 0 and (SELECT max(salary)/5 from employees)
于 2012-07-25T05:20:13.747 回答
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;

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

于 2012-07-25T07:25:12.323 回答