Hi. I've done some searching on this and I've tried a few pen-and-pencil operations, but I keep getting stuck in my "worst case" scenario.
My scenario:
I am working on an Oracle 11g database with query access.
I have a table of job records indicating the start and end date of each job and which employee ID held that job.
I have a table of employee records that shows
- original hire date
- current hire date (most recent employment)
- adjusted date
The adjusted date is what is supposed to be the earliest date of consecutive employment. Note that "consecutive" in this case is defined as having no more than a 1-year gap of employment.
Of course, this field has not always been used correctly and I am trying to find the aberrant records.
Let us assume that an employee had 4 jobs with the company:
- 1: 01/01/00 to 01/01/01
- 2: 06/01/02 to 01/01/04
- 3: 03/01/02 to 06/30/02
- 4: 02/01/04 to [null]
The correct Adjusted Date for this employee would be 03/01/02 because there is only a one month gap between jobs 4 and 2 and there is an overlap between jobs 2 and 3.
So far, I have created a subquery for the current job record, currentJob, with all of the key elements. My original instinct was to create a second table with all of the other jobs (or all of the jobs, for that matter) and look for the MIN(START_DATE) where oldJobs.END_DATE >= (currentJob.START_DATE - 365), but of course that won't catch a situation like the above.
I'm starting to think that perhaps I could write a recursive query starting with the current job then iterating through the other jobs, but I've tried the START WITH...CONNECT BY PRIOR syntax to no avail.
For example:
Code:
emp.ADJ_SERVICE_DATE <
(SELECT MIN (jobs.JOB_BEGIN)
FROM (SELECT DISTINCT
PIDM,
JOB_BEGIN,
JOB_END,
POSN,
STATUS,
BCAT,
JOB_TYPE
FROM IDENTITY_TABLE sp
INNER JOIN {several tables}
ORDER BY JOB_END DESC) jobs
START WITH jobs.JOB_END = NULL
CONNECT BY jobs.JOB_BEGIN <= PRIOR jobs.JOB_END + 365 )
...returns 0 records.
I have verified that my inner jobs query (abbreviated here) works to return 58 potential issues (adjusted date < current hire date), and 2 or 3 legitimate issues (adjusted date < earliest consecutive job start date).
If anyone has any pointers or insight, I'd be glad to hear it. I've never tried recursion in Oracle and I don't quite know if I understand the syntax even after reading several message boards and the Oracle documentation.
Am I doing this right? Will it properly iterate the way I have it set up? Is there a better way to compare sets of dates as described above?
Thanks,
~Mike