Skip to Main Content

Fetching Data Effective date wise by joining three tables

Vemula MuniAug 13 2019 — edited Aug 20 2019

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
EMPLIDBUSINESS_UNITEMPL_RCDEFFDTEFFSEQACTION
733930US410027-May-20190ADD
733930US410002-Jun-20190DTA
733930US410004-Jun-20190TER
733930US410106-Jun-20190HIR
733930US410115-Jun-20190TER
733930US410215-Jun-20191ADD
733930US608219-Jun-20190ASC
733930CN512226-Jun-20190ASG
733931US410026-Apr-20190HIR
733931US410005-Jun-20190TER
733931IN208106-Jun-20190ADD
733931IN208110-Jun-20190TER
733931IN208212-Jun-20190HIR
733931IN408218-Jun-20190ASC
733931IN408219-Jun-20190TER
733931IN208326-Jun-20190ADD
733931IN208328-Jun-20190DTA

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_IDEMPL_RCDEFFDTEFFSEQHR_STATUSPER_ORG
733930027-May-20190ACWR
733930002-Jun-20190ACWR
733930004-Jun-20190ICWR
733930106-Jun-20190AEMP
733930115-Jun-20190IEMP
733930215-Jun-20191ACWR
733930219-Jun-20190ACWR
733930226-Jun-20190ACWR
733931026-Apr-20190AEMP
733931005-Jun-20190IEMP
733931106-Jun-20190ACWR
733931110-Jun-20190ICWR
733931212-Jun-20190AEMP
733931218-Jun-20190AEMP
733931219-Jun-20190IEMP
733931326-Jun-20190ACWR
733931328-Jun-20190ADTA

Below is my "PS_CTS_IQN_BU"  table structure and data.

IQN BU Setp
CountryBusiness_Unit
USAUS410
USAUS412
CANCN512
INDIN208

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.

  1. When ever employee is active as CWR and business unit value is available in IQN_BU table We need to fetch that employee information.
  2. 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'.
  3. 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.
  4. When there is consecutive rows with HR_STATUS ='A' then we need to break from date and to dates for each row.

EMPLIDFROM_DATETo_DATEPER_ORGHR_STATUSBusiness_UnitActionEFFDTReason
73393001-Jun-201901-Jun-2019CWRAUS410ADD27-May-2019Employee active as CWR
73393002-Jun-201903-Jun-2019CWRAUS410DTA02-Jun-2019Employee active as CWR
73393015-Jun-201918-Jun-2019CWRAUS410ADD15-Jun-2019Employee active as CWR
73393026-Jun-201930-Jun-2019CWRACN512ASG26-Jun-2019Employee active as CWR
73393106-Jun-201909-Jun-2019CWRAIN208ADD06-Jun-2019Employee active as CWR
73393126-Jun-201927-Jun-2019CWRAIN208ADD26-Jun-2019Employee active as CWR
73393128-Jun-201930-Jun-2019CWRAIN208LOC28-Jun-2019Employee active as CWR

This post has been answered by Frank Kulash on Aug 13 2019
Jump to Answer
Comments
Post Details
Added on Aug 13 2019
14 comments
775 views