DBA Version : Oracle Database 12c Enterprise Edition Release 12.1.0.2.0 - 64bit Production
Below is my "PS_CTS_JOB_DTL" table structure and data. in this table (EMPLID,EMPL_RCD,EFFDT,EFFSEQ) are key fields.
EMPL_RCD : when ever employee converted from contract to full time and from full time to contract this value will increase. (0,1,2,...)
EFFDT : this filed use to track when change happens.
EFFSEQ : this field will increase from 0 to 1,2 .. when there is more than one transaction in same date.( with same effdt )
ACTION : Will determine which action they taken on employee ( please don't consider action field to achieve output as below. because action codes may different in each time)
Note : Data Volume of this table is more than 50 million rows
PS_CTS_JOB_DTL Table |
EMPLID | BUSINESS_UNIT | EMPL_RCD | EFFDT | EFFSEQ | ACTION |
733930 | US410 | 0 | 27-May-2019 | 0 | ADD |
733930 | US410 | 0 | 02-Jun-2019 | 0 | DTA |
733930 | US410 | 0 | 04-Jun-2019 | 0 | TER |
733930 | US410 | 1 | 06-Jun-2019 | 0 | HIR |
733930 | US410 | 1 | 15-Jun-2019 | 0 | TER |
733930 | US410 | 2 | 15-Jun-2019 | 1 | ADD |
733930 | US608 | 2 | 19-Jun-2019 | 0 | ASC |
733930 | CN512 | 2 | 26-Jun-2019 | 0 | ASG |
733931 | US410 | 0 | 26-Apr-2019 | 0 | HIR |
733931 | US410 | 0 | 05-Jun-2019 | 0 | TER |
733931 | IN208 | 1 | 06-Jun-2019 | 0 | ADD |
733931 | IN208 | 1 | 10-Jun-2019 | 0 | TER |
733931 | IN208 | 2 | 12-Jun-2019 | 0 | HIR |
733931 | IN408 | 2 | 18-Jun-2019 | 0 | ASC |
733931 | IN408 | 2 | 19-Jun-2019 | 0 | TER |
733931 | IN208 | 3 | 26-Jun-2019 | 0 | ADD |
733931 | IN208 | 3 | 28-Jun-2019 | 0 | DTA |
PS_CTS_JOB_DTL1 Table :
Note : Data Volume of this table is more than 50 million rows
Below is my "PS_CTS_JOB_DTL1" table structure and data. in this table (EMPLID,EMPL_RCD,EFFDT,EFFSEQ) are key fields.
EMPL_RCD : when ever employee converted from contract to full time and from full time to contract this value will increase. (0,1,2,...)
EFFDT : this filed use to track when change happens.
EFFSEQ : this field will increase from 0 to 1,2 .. when there is more than one transaction in same date.( with same effdt )
HR_STATUS : To identify employee status ( A = Active, I = Inactive ).
PER_ORG : to identify employee type ( CWR = Contract, EMP = Employee )
Note : employee may contains more then one row with HR_STATUS = 'A' before he is going to inactive ( HR_STSTUS = 'I' )
EMPL_ID | EMPL_RCD | EFFDT | EFFSEQ | HR_STATUS | PER_ORG |
733930 | 0 | 27-May-2019 | 0 | A | CWR |
733930 | 0 | 02-Jun-2019 | 0 | A | CWR |
733930 | 0 | 04-Jun-2019 | 0 | I | CWR |
733930 | 1 | 06-Jun-2019 | 0 | A | EMP |
733930 | 1 | 15-Jun-2019 | 0 | I | EMP |
733930 | 2 | 15-Jun-2019 | 1 | A | CWR |
733930 | 2 | 19-Jun-2019 | 0 | A | CWR |
733930 | 2 | 26-Jun-2019 | 0 | A | CWR |
733931 | 0 | 26-Apr-2019 | 0 | A | EMP |
733931 | 0 | 05-Jun-2019 | 0 | I | EMP |
733931 | 1 | 06-Jun-2019 | 0 | A | CWR |
733931 | 1 | 10-Jun-2019 | 0 | I | CWR |
733931 | 2 | 12-Jun-2019 | 0 | A | EMP |
733931 | 2 | 18-Jun-2019 | 0 | A | EMP |
733931 | 2 | 19-Jun-2019 | 0 | I | EMP |
733931 | 3 | 26-Jun-2019 | 0 | A | CWR |
733931 | 3 | 28-Jun-2019 | 0 | A | DTA |
Below is my "PS_CTS_IQN_BU" table structure and data.
IQN BU Setp |
Country | Business_Unit |
USA | US410 |
USA | US412 |
CAN | CN512 |
IND | IN208 |
Required Output based on above data :
When i pass the start date as TO_DATE ('01-JUN-2019', 'DD-MON-YYYY') and end date as TO_DATE ('30-JUN-2019', 'DD-MON-YYYY') Query should fetch the below data.
- When ever employee is active as CWR and business unit value is available in IQN_BU table We need to fetch that employee information.
- In below output first row from date and to date will be '01-JUN-2019' because there is a row in JOB_DTL table with effective date '02-JUN-2019'.
- in second row employee active as CWR with new row 02-JUN-2019 to 03-JUN-2019 because he is inactive on 04-JUN-2019.
- When there is consecutive rows with HR_STATUS ='A' then we need to break from date and to dates for each row.
EMPLID | FROM_DATE | To_DATE | PER_ORG | HR_STATUS | Business_Unit | Action | EFFDT | Reason |
733930 | 01-Jun-2019 | 01-Jun-2019 | CWR | A | US410 | ADD | 27-May-2019 | Employee active as CWR |
733930 | 02-Jun-2019 | 03-Jun-2019 | CWR | A | US410 | DTA | 02-Jun-2019 | Employee active as CWR |
733930 | 15-Jun-2019 | 18-Jun-2019 | CWR | A | US410 | ADD | 15-Jun-2019 | Employee active as CWR |
733930 | 26-Jun-2019 | 30-Jun-2019 | CWR | A | CN512 | ASG | 26-Jun-2019 | Employee active as CWR |
733931 | 06-Jun-2019 | 09-Jun-2019 | CWR | A | IN208 | ADD | 06-Jun-2019 | Employee active as CWR |
733931 | 26-Jun-2019 | 27-Jun-2019 | CWR | A | IN208 | ADD | 26-Jun-2019 | Employee active as CWR |
733931 | 28-Jun-2019 | 30-Jun-2019 | CWR | A | IN208 | LOC | 28-Jun-2019 | Employee active as CWR |