开发者

ORACLE: ORA-00904: : invalid identifier

I have created following selec开发者_如何学Pythont:

select e.first_name, e.last_name, 
   (select jobname_id from 
       (select j.id, j.jobname_id, first_value(j.jobname_id) 
        over (order by j.date_from desc) as current_job 
        from jobs j where j.emp_id=e.emp_id and j.date_from < sysdate) 
    where jobname_id != current_job and rownum=1) as previous_job 
from employees e

but I get "ORA-00904: "E"."EMP_ID": invalid identifier"

how can I use the reference to E.EMP_ID in the subquery?


I don't know if this solution ist fast enough for your needs but it should work as planned. Also, I agree with Ollie about the additional field "date_to". It would make things easier.

select
  e.first_name   as first_name,
  e.last_name    as last_name,
  sub.jobname_id as jobname_id
from 
  employees e,
  (
    select
      j.emp_id     as emp_id,
      j.jobname_id as jobname_id,
      row_number()
        over (
          partition by j.emp_id
          order by     j.date_from desc
        ) as job_order
    from
      jobs j
    where j.date_from < sysdate
  ) sub
where sub.emp_id    = e.emp_id
  and sub.job_order = 2


You can't, but you can move the join out into the outer subquery. Not sure if it works out well with the analytical function, though.

select e.first_name, e.last_name, 
   (select jobname_id from 
       (select j.emp_id, j.id, j.jobname_id, first_value(j.jobname_id) 
        over (order by j.date_from desc) as current_job 
        from jobs j where j.date_from < sysdate) j
    where j.emp_id=e.emp_id and jobname_id != current_job and rownum=1) as previous_job 
from employees e
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜