سؤال

I have created following select:

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
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top