伙计们,我必须选择每个员工,他的薪水比他所在部门的薪水多,有一个代码可以解决这个问题,就在这里
select * from
(select e.first_name || ' ' || e.last_name ENAME,
d.department_name,e.salary, trunc(e.salary-avg(salary) over (partition by
e.department_id)) sal_dif
from employees e,departments d
where e.department_id=d.department_id)
where sal_dif>0
当我运行它时,它工作正常,而且这个代码的成本在我的计算机中是6,我试图用另一种方式来做,像这样
select first_name || ' '|| last_name,salary
from
(select first_name || ' ' || last_name,salary ,avg(salary) over (partition by department_id) avg_salary
from employees )
where salary>avg_salary;
我很惊讶,尽管数据库中存在last_name和first_name,但错误
shows me ORA-00904: "LAST_NAME": invalid identifier
00904. 00000 - "%s: invalid identifier"
*Cause:
*Action:
Error at Line: 8 Column: 29
为什么?我可以用其他方式重写第一个代码吗?非常感谢
答案 0 :(得分:1)
select name,salary
from
(select first_name || ' ' || last_name as name,salary ,avg(salary) over (partition by department_id) avg_salary
from employees )
where salary>avg_salary;
答案 1 :(得分:1)
使用别名来标识列:
select full_name,salary
from
(select first_name || ' ' || last_name as full_name,salary ,avg(salary) over (partition by department_id) avg_salary
from employees )
where salary>avg_salary;
答案 2 :(得分:1)
作为在子查询中对组合列进行别名的替代方法,不要将子查询中的列组合在一起 - 而是将连接替换为,
,如下所示:
select first_name || ' '|| last_name,salary
from
(select first_name, last_name, salary,
avg(salary) over (partition by department_id) avg_salary
from employees)
where salary>avg_salary;