Discussions
Categories
- 385.5K All Categories
- 4.9K Data
- 2.5K Big Data Appliance
- 2.4K Data Science
- 453.4K Databases
- 223.2K General Database Discussions
- 3.8K Java and JavaScript in the Database
- 47 Multilingual Engine
- 606 MySQL Community Space
- 486 NoSQL Database
- 7.9K Oracle Database Express Edition (XE)
- 3.2K ORDS, SODA & JSON in the Database
- 585 SQLcl
- 4K SQL Developer Data Modeler
- 188K SQL & PL/SQL
- 21.5K SQL Developer
- 46 Data Integration
- 46 GoldenGate
- 298.4K Development
- 4 Application Development
- 20 Developer Projects
- 166 Programming Languages
- 295K Development Tools
- 150 DevOps
- 3.1K QA/Testing
- 646.7K Java
- 37 Java Learning Subscription
- 37.1K Database Connectivity
- 201 Java Community Process
- 108 Java 25
- 22.2K Java APIs
- 138.3K Java Development Tools
- 165.4K Java EE (Java Enterprise Edition)
- 22 Java Essentials
- 176 Java 8 Questions
- 86K Java Programming
- 82 Java Puzzle Ball
- 65.1K New To Java
- 1.7K Training / Learning / Certification
- 13.8K Java HotSpot Virtual Machine
- 94.3K Java SE
- 13.8K Java Security
- 208 Java User Groups
- 25 JavaScript - Nashorn
- Programs
- 667 LiveLabs
- 41 Workshops
- 10.3K Software
- 6.7K Berkeley DB Family
- 3.6K JHeadstart
- 6K Other Languages
- 2.3K Chinese
- 207 Deutsche Oracle Community
- 1.1K Español
- 1.9K Japanese
- 474 Portuguese
Fetching Contract Employee information by Join the JOB_DTL,JOB_DTL1,IQN_BU Tables.

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)
JOB Table | |||||
EMPLID | BUSINESS_UNIT | EMPL_RCD | EFFDT | EFFSEQ | ACTION |
733930 | US410 | 0 | 27-May-19 | 0 | ADD |
733930 | US410 | 0 | 02-Jun-19 | 0 | DTA |
733930 | US410 | 0 | 4-Jun-19 | 0 | TER |
733930 | US410 | 1 | 6-Jun-19 | 0 | HIR |
733930 | US410 | 1 | 15-Jun-19 | 0 | TER |
733930 | US410 | 2 | 15-Jun-19 | 1 | ADD |
733930 | US608 | 2 | 19-Jun-19 | 0 | ASC |
733930 | CN512 | 2 | 26-Jun-19 | 0 | ASG |
733931 | US410 | 0 | 26-Apr-19 | 0 | HIR |
733931 | US410 | 0 | 5-Jun-19 | 0 | TER |
733931 | IN208 | 1 | 6-Jun-19 | 0 | ADD |
733931 | IN208 | 1 | 10-Jun-19 | 0 | TER |
733931 | IN208 | 2 | 12-Jun-19 | 0 | HIR |
733931 | IN408 | 2 | 18-Jun-19 | 0 | ASC |
733931 | IN408 | 2 | 19-Jun-19 | 0 | TER |
733931 | IN208 | 3 | 26-Jun-19 | 0 | ADD |
733931 | IN208 | 3 | 28-Jun-19 | 0 | DTA |
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 )
EMPL_ID | EMPL_RCD | EFFDT | EFFSEQ | HR_STATUS | PER_ORG |
733930 | 0 | 27-May-19 | 0 | A | CWR |
733930 | 0 | 02-Jun-19 | 0 | A | CWR |
733930 | 0 | 4-Jun-19 | 0 | I | CWR |
733930 | 1 | 6-Jun-19 | 0 | A | EMP |
733930 | 1 | 15-Jun-19 | 0 | I | EMP |
733930 | 2 | 15-Jun-19 | 1 | A | CWR |
733930 | 2 | 19-Jun-19 | 0 | A | CWR |
733930 | 2 | 26-Jun-19 | 0 | A | CWR |
733931 | 0 | 26-Apr-19 | 0 | A | EMP |
733931 | 0 | 5-Jun-19 | 0 | I | EMP |
733931 | 1 | 6-Jun-19 | 0 | A | CWR |
733931 | 1 | 10-Jun-19 | 0 | I | CWR |
733931 | 2 | 12-Jun-19 | 0 | A | EMP |
733931 | 2 | 18-Jun-19 | 0 | A | EMP |
733931 | 2 | 19-Jun-19 | 0 | I | EMP |
733931 | 3 | 26-Jun-19 | 0 | A | CWR |
733931 | 3 | 28-Jun-19 | 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 |
Based on above three tables i need to fetch below output. When i pass from date and to date in query (month start and end dates i.e like '01-JUN-19' and '30-JUN-19' ) , query should fetch employee active from active to dates as CWR and that employee Business unit should be in PS_CTS_IQN_BU table.
Required output
Note : Reason column for understanding, no need to display reason column
While fetching below out put Please map EMPLID,EMPL_RCD,EFFDT,EFFSEQ Key fields in both JOB_DTL and JOB_DTL1 ( Map Key fields is very important )
Employee 733930 is active as CWR in between '19-JUN-19' to '25-JUN-19' in different BU which not in IQN_BU setup table, so i want to exclude this information.
EMPLID | FROM_DATE | To_DATE | PER_ORG | HR_STATUS | Reason |
733930 | 1-Jun-19 | 3-Jun-19 | CWR | A | Employee is active as CWR and BU is in IQN_BU setup table |
733930 | 15-Jun-19 | 18-Jun-19 | CWR | A | Employee is active as CWR and BU is in IQN_BU setup table |
733930 | 26-Jun-19 | 30-Jun-19 | CWR | A | Employee is active as CWR and BU is in IQN_BU setup table |
733931 | 6-Jun-19 | 9-Jun-19 | CWR | A | Employee is active as CWR and BU is in IQN_BU setup table |
733931 | 26-Jun-19 | 30-JUN-19 | CWR | A | Employee is active as CWR and BU is in IQN_BU setup table |
PS_CTS_JOB_DTL Table Build and Insert Scripts:
CREATE TABLE PS_CTS_JOB_DTL (EMPLID VARCHAR2(11) NOT NULL, BUSINESS_UNIT VARCHAR2(5) NOT NULL, EMPL_RCD INT NOT NULL, EFFDT DATE, EFFSEQ INT NOT NULL, ACTION VARCHAR2(3) NOT NULL) ;Insert into PS_CTS_JOB_DTL (EMPLID,BUSINESS_UNIT,EMPL_RCD,EFFDT,EFFSEQ,ACTION) values ('733930','US410',0,to_date('27-MAY-19','DD-MON-YY'),0,'ADD');Insert into PS_CTS_JOB_DTL (EMPLID,BUSINESS_UNIT,EMPL_RCD,EFFDT,EFFSEQ,ACTION) values ('733930','US410',0,to_date('02-JUN-19','DD-MON-YY'),0,'DTA');Insert into PS_CTS_JOB_DTL (EMPLID,BUSINESS_UNIT,EMPL_RCD,EFFDT,EFFSEQ,ACTION) values ('733930','US410',0,to_date('04-JUN-19','DD-MON-YY'),0,'TER');Insert into PS_CTS_JOB_DTL (EMPLID,BUSINESS_UNIT,EMPL_RCD,EFFDT,EFFSEQ,ACTION) values ('733930','US410',1,to_date('06-JUN-19','DD-MON-YY'),0,'HIR');Insert into PS_CTS_JOB_DTL (EMPLID,BUSINESS_UNIT,EMPL_RCD,EFFDT,EFFSEQ,ACTION) values ('733930','US410',1,to_date('15-JUN-19','DD-MON-YY'),0,'TER');Insert into PS_CTS_JOB_DTL (EMPLID,BUSINESS_UNIT,EMPL_RCD,EFFDT,EFFSEQ,ACTION) values ('733930','US410',2,to_date('15-JUN-19','DD-MON-YY'),1,'ADD');Insert into PS_CTS_JOB_DTL (EMPLID,BUSINESS_UNIT,EMPL_RCD,EFFDT,EFFSEQ,ACTION) values ('733930','US608',2,to_date('19-JUN-19','DD-MON-YY'),0,'ASC');Insert into PS_CTS_JOB_DTL (EMPLID,BUSINESS_UNIT,EMPL_RCD,EFFDT,EFFSEQ,ACTION) values ('733930','CN512',2,to_date('26-JUN-19','DD-MON-YY'),0,'ASG');Insert into PS_CTS_JOB_DTL (EMPLID,BUSINESS_UNIT,EMPL_RCD,EFFDT,EFFSEQ,ACTION) values ('733931','US410',0,to_date('26-APR-19','DD-MON-YY'),0,'HIR');Insert into PS_CTS_JOB_DTL (EMPLID,BUSINESS_UNIT,EMPL_RCD,EFFDT,EFFSEQ,ACTION) values ('733931','US410',0,to_date('05-JUN-19','DD-MON-YY'),0,'TER');Insert into PS_CTS_JOB_DTL (EMPLID,BUSINESS_UNIT,EMPL_RCD,EFFDT,EFFSEQ,ACTION) values ('733931','IN208',1,to_date('06-JUN-19','DD-MON-YY'),1,'ADD');Insert into PS_CTS_JOB_DTL (EMPLID,BUSINESS_UNIT,EMPL_RCD,EFFDT,EFFSEQ,ACTION) values ('733931','IN208',1,to_date('10-JUN-19','DD-MON-YY'),0,'TER');Insert into PS_CTS_JOB_DTL (EMPLID,BUSINESS_UNIT,EMPL_RCD,EFFDT,EFFSEQ,ACTION) values ('733931','IN208',2,to_date('12-JUN-19','DD-MON-YY'),0,'HIR');Insert into PS_CTS_JOB_DTL (EMPLID,BUSINESS_UNIT,EMPL_RCD,EFFDT,EFFSEQ,ACTION) values ('733931','IN408',2,to_date('18-JUN-19','DD-MON-YY'),0,'ASC');Insert into PS_CTS_JOB_DTL (EMPLID,BUSINESS_UNIT,EMPL_RCD,EFFDT,EFFSEQ,ACTION) values ('733931','IN408',2,to_date('19-JUN-19','DD-MON-YY'),0,'TER');Insert into PS_CTS_JOB_DTL (EMPLID,BUSINESS_UNIT,EMPL_RCD,EFFDT,EFFSEQ,ACTION) values ('733931','IN208',3,to_date('26-JUN-19','DD-MON-YY'),0,'ADD');Insert into PS_CTS_JOB_DTL (EMPLID,BUSINESS_UNIT,EMPL_RCD,EFFDT,EFFSEQ,ACTION) values ('733931','IN208',3,to_date('28-JUN-19','DD-MON-YY'),0,'DTA');
PS_CTS_JOB_DTL1 Table Build and Insert Scripts:
CREATE TABLE PS_CTS_JOB_DTL1 (EMPLID VARCHAR2(11) NOT NULL, EMPL_RCD INT NOT NULL, EFFDT DATE, EFFSEQ INT NOT NULL, HR_STATUS VARCHAR2(1) NOT NULL, PER_ORG VARCHAR2(3) NOT NULL) ;Insert into PS_CTS_JOB_DTL1 (EMPLID,EMPL_RCD,EFFDT,EFFSEQ,HR_STATUS,PER_ORG) values ('733930',0,to_date('27-MAY-19','DD-MON-YY'),0,'A','CWR');Insert into PS_CTS_JOB_DTL1 (EMPLID,EMPL_RCD,EFFDT,EFFSEQ,HR_STATUS,PER_ORG) values ('733930',0,to_date('02-JUN-19','DD-MON-YY'),0,'A','CWR');Insert into PS_CTS_JOB_DTL1 (EMPLID,EMPL_RCD,EFFDT,EFFSEQ,HR_STATUS,PER_ORG) values ('733930',0,to_date('04-JUN-19','DD-MON-YY'),0,'I','CWR');Insert into PS_CTS_JOB_DTL1 (EMPLID,EMPL_RCD,EFFDT,EFFSEQ,HR_STATUS,PER_ORG) values ('733930',1,to_date('06-JUN-19','DD-MON-YY'),0,'A','EMP');Insert into PS_CTS_JOB_DTL1 (EMPLID,EMPL_RCD,EFFDT,EFFSEQ,HR_STATUS,PER_ORG) values ('733930',1,to_date('15-JUN-19','DD-MON-YY'),0,'I','EMP');Insert into PS_CTS_JOB_DTL1 (EMPLID,EMPL_RCD,EFFDT,EFFSEQ,HR_STATUS,PER_ORG) values ('733930',2,to_date('15-JUN-19','DD-MON-YY'),1,'A','CWR');Insert into PS_CTS_JOB_DTL1 (EMPLID,EMPL_RCD,EFFDT,EFFSEQ,HR_STATUS,PER_ORG) values ('733930',2,to_date('19-JUN-19','DD-MON-YY'),0,'A','CWR');Insert into PS_CTS_JOB_DTL1 (EMPLID,EMPL_RCD,EFFDT,EFFSEQ,HR_STATUS,PER_ORG) values ('733930',2,to_Date('26-JUN-19','DD-MON-YY'),0,'A','CWR');Insert into PS_CTS_JOB_DTL1 (EMPLID,EMPL_RCD,EFFDT,EFFSEQ,HR_STATUS,PER_ORG) values ('733931',0,to_date('26-APR-19','DD-MON-YY'),0,'A','EMP');Insert into PS_CTS_JOB_DTL1 (EMPLID,EMPL_RCD,EFFDT,EFFSEQ,HR_STATUS,PER_ORG) values ('733931',0,to_date('05-JUN-19','DD-MON-YY'),0,'I','EMP');Insert into PS_CTS_JOB_DTL1 (EMPLID,EMPL_RCD,EFFDT,EFFSEQ,HR_STATUS,PER_ORG) values ('733931',1,to_date('06-JUN-19','DD-MON-YY'),0,'A','CWR');Insert into PS_CTS_JOB_DTL1 (EMPLID,EMPL_RCD,EFFDT,EFFSEQ,HR_STATUS,PER_ORG) values ('733931',1,to_date('10-JUN-19','DD-MON-YY'),0,'I','CWR');Insert into PS_CTS_JOB_DTL1 (EMPLID,EMPL_RCD,EFFDT,EFFSEQ,HR_STATUS,PER_ORG) values ('733931',2,to_date('12-JUN-19','DD-MON-YY'),0,'A','EMP');Insert into PS_CTS_JOB_DTL1 (EMPLID,EMPL_RCD,EFFDT,EFFSEQ,HR_STATUS,PER_ORG) values ('733931',2,to_date('18-JUN-19','DD-MON-YY'),0,'A','EMP');Insert into PS_CTS_JOB_DTL1 (EMPLID,EMPL_RCD,EFFDT,EFFSEQ,HR_STATUS,PER_ORG) values ('733931',2,to_date('19-JUN-19','DD-MON-YY'),0,'I','EMP');Insert into PS_CTS_JOB_DTL1 (EMPLID,EMPL_RCD,EFFDT,EFFSEQ,HR_STATUS,PER_ORG) values ('733931',3,to_date('26-JUN-19','DD-MON-YY'),0,'A','CWR');Insert into PS_CTS_JOB_DTL1 (EMPLID,EMPL_RCD,EFFDT,EFFSEQ,HR_STATUS,PER_ORG) values ('733931',3,to_date('28-JUN-19','DD-MON-YY'),0,'A','CWR');
PS_CTS_IQN_BU Table Build and Insert Scripts: ( IQN business unit setup )
CREATE TABLE PS_CTS_IQN_BU (COUNTRY VARCHAR2(3) NOT NULL, BUSINESS_UNIT VARCHAR2(5) NOT NULL) ;Insert into PS_CTS_IQN_BU (COUNTRY,BUSINESS_UNIT) values ('USA','US410');Insert into PS_CTS_IQN_BU (COUNTRY,BUSINESS_UNIT) values ('USA','US412');Insert into PS_CTS_IQN_BU (COUNTRY,BUSINESS_UNIT) values ('CAN','CN512');Insert into PS_CTS_IQN_BU (COUNTRY,BUSINESS_UNIT) values ('IND','IN208');
Best Answer
-
Hi,
Since you're using Oracle 12, you can take advantage of the OUTER APPLY feature. Here's one way:
WITH parameters AS( SELECT TO_DATE ('01-JUN-2019', 'DD-MON-YYYY') AS startdt -- Always use 4 digits for years , TO_DATE ('30-JUN-2019', 'DD-MON-YYYY') AS enddt , 'CWR' AS per_org FROM dual), got_business_unit AS( SELECT p.startdt, p.enddt , d1.emplid, d1.per_org, d1.effdt , CASE WHEN b.business_unit IS NULL THEN 'I' ELSE d1.hr_status END AS hr_status , b.business_unit FROM parameters p JOIN ps_cts_job_dtl1 d1 ON d1.per_org = p.per_org AND d1.effdt <= p.enddt OUTER APPLY ( SELECT MAX (b.business_unit) AS business_unit FROM ps_cts_job_dtl d JOIN ps_cts_iqn_bu b ON b.business_unit = d.business_unit WHERE d.emplid = d1.emplid AND d.effdt = d1.effdt ) b)SELECT g.emplid, GREATEST ( g.effdt , g.startdt ) AS startdt, LEAST ( e.enddt , g.enddt ) AS enddt, g.per_org, g.hr_status, 'Employee is active as CWR and BU is in IQN_BU setup table' AS reasonFROM got_business_unit gOUTER APPLY ( SELECT NVL ( MIN (gs.effdt) - 1 , DATE '9999-12-31' ) AS enddt FROM got_business_unit gs WHERE gs.emplid = g.emplid AND gs.effdt > g.effdt AND gs.hr_status = 'I' ) eWHERE hr_status = 'A'AND e.enddt > g.startdtORDER BY emplid, startdt;
The output from your sample data is just what you requested:
EMPLID STARTDT ENDDT PER_ORG HR_STATUS REASON----------- ----------- ----------- ------- --------- ---------------------------------------------------------733930 01-Jun-2019 03-Jun-2019 CWR A Employee is active as CWR and BU is in IQN_BU setup table733930 15-Jun-2019 18-Jun-2019 CWR A Employee is active as CWR and BU is in IQN_BU setup table733930 26-Jun-2019 30-Jun-2019 CWR A Employee is active as CWR and BU is in IQN_BU setup table733931 06-Jun-2019 09-Jun-2019 CWR A Employee is active as CWR and BU is in IQN_BU setup table733931 26-Jun-2019 30-Jun-2019 CWR A Employee is active as CWR and BU is in IQN_BU setup table
You want a row of output for each row in the table that has hr_status='A' (and is in the given date range, and has the given per_org). However, if a row does not have any matching row in ps_cts_iqn_bu, then you want to count its hr_status as 'I'.
The sub-query called b above finds one of the matching business_units in ps_cts_iqn_bu, if there is one. It's not important which one it finds, you just need to know if it found any.
The sub-query called e finds the 'I' row corresponding to each 'A' row, that is, the earliest 'I' row matching each 'A' row. If there is no matching 'I' row, that means the emplid is still active, so act as if the inactive date is far in the future.
Answers
-
Hi,
Would you like to get answers that work? Then make sure the CREATE TABLE and INSERT statements you post work, too. Test (and, if necessary, fix) them before you post them. A lot of the INSERT statements you posted fail.
Also make sure the results you post are exactly what you want. Do you want a row that has hr_status='This Row not required ...'? If that was just a comment, don't include it in the result set; put comments before or after the results. Do you really want "31-JUN" as a date?
-
PS_CTS_JOB_DTL Table Build and Insert Scripts:- CREATETABLEPS_CTS_JOB_DTL(EMPLIDVARCHAR2(11)NOTNULL,
- BUSINESS_UNITVARCHAR2(5)NOTNULL,
- EMPL_RCDSMALLINTNOTNULL,
- EFFDTDATE,
- EFFSEQSMALLINTNOTNULL,
- ACTIONVARCHAR2(3)NOTNULL);
- InsertintoPS_CTS_JOB_DTL(EMPLID,BUSINESS_UNIT,EMPL_RCD,EFFDT,EFFSEQ,ACTION,ACTION_DT)values('733930','US410',0,to_date('27-MAY-19','DD-MON-YY'),0,'ADD');
'Those who cannot remember the past are condemned to repeat it.' (George Santayana-1905)
Does the term "Y2K" mean anything to you? Do you remember 1999 and 2000?
20 years ago I and armies of guys like me were busting our butts to insure that most of the computer programs in the world did not break shortly after 12:01 AM on January 1, 2000.
TWENTY ******* YEARS AGO!
It's long, long, long past time to quit using 2-digit years and specify 4-digit years. Every time. EVERY TIME!
-
Hi @Frank Kulash, Create table and insert statements are updated.
-
Hi,
Vemula Muni wrote:Hi Frank Kulash, Create table and insert statements are updated.
Okay; what about the desired results? If you really want '31-JUN-19' in the output, then explain.
(I bet that's a mistake, but if you posted that mistake even after it was pointed out in reply #1, then there's a good chance the posted results have other mistakes.)
Employee is active as CWR and BU is in IQN_BU setup table -
Hi @Frank Kulash , Sorry for the incorrect date. It should be 30-Jun-19. Edited the same.
-
Hi,
Since you're using Oracle 12, you can take advantage of the OUTER APPLY feature. Here's one way:
WITH parameters AS( SELECT TO_DATE ('01-JUN-2019', 'DD-MON-YYYY') AS startdt -- Always use 4 digits for years , TO_DATE ('30-JUN-2019', 'DD-MON-YYYY') AS enddt , 'CWR' AS per_org FROM dual), got_business_unit AS( SELECT p.startdt, p.enddt , d1.emplid, d1.per_org, d1.effdt , CASE WHEN b.business_unit IS NULL THEN 'I' ELSE d1.hr_status END AS hr_status , b.business_unit FROM parameters p JOIN ps_cts_job_dtl1 d1 ON d1.per_org = p.per_org AND d1.effdt <= p.enddt OUTER APPLY ( SELECT MAX (b.business_unit) AS business_unit FROM ps_cts_job_dtl d JOIN ps_cts_iqn_bu b ON b.business_unit = d.business_unit WHERE d.emplid = d1.emplid AND d.effdt = d1.effdt ) b)SELECT g.emplid, GREATEST ( g.effdt , g.startdt ) AS startdt, LEAST ( e.enddt , g.enddt ) AS enddt, g.per_org, g.hr_status, 'Employee is active as CWR and BU is in IQN_BU setup table' AS reasonFROM got_business_unit gOUTER APPLY ( SELECT NVL ( MIN (gs.effdt) - 1 , DATE '9999-12-31' ) AS enddt FROM got_business_unit gs WHERE gs.emplid = g.emplid AND gs.effdt > g.effdt AND gs.hr_status = 'I' ) eWHERE hr_status = 'A'AND e.enddt > g.startdtORDER BY emplid, startdt;
The output from your sample data is just what you requested:
EMPLID STARTDT ENDDT PER_ORG HR_STATUS REASON----------- ----------- ----------- ------- --------- ---------------------------------------------------------733930 01-Jun-2019 03-Jun-2019 CWR A Employee is active as CWR and BU is in IQN_BU setup table733930 15-Jun-2019 18-Jun-2019 CWR A Employee is active as CWR and BU is in IQN_BU setup table733930 26-Jun-2019 30-Jun-2019 CWR A Employee is active as CWR and BU is in IQN_BU setup table733931 06-Jun-2019 09-Jun-2019 CWR A Employee is active as CWR and BU is in IQN_BU setup table733931 26-Jun-2019 30-Jun-2019 CWR A Employee is active as CWR and BU is in IQN_BU setup table
You want a row of output for each row in the table that has hr_status='A' (and is in the given date range, and has the given per_org). However, if a row does not have any matching row in ps_cts_iqn_bu, then you want to count its hr_status as 'I'.
The sub-query called b above finds one of the matching business_units in ps_cts_iqn_bu, if there is one. It's not important which one it finds, you just need to know if it found any.
The sub-query called e finds the 'I' row corresponding to each 'A' row, that is, the earliest 'I' row matching each 'A' row. If there is no matching 'I' row, that means the emplid is still active, so act as if the inactive date is far in the future. -
Hi @Frank Kulash, According to above data employee 733930 is active from 01-JUN-2019 to 03-JUN-19. after that i am inserted one more row with effdt '02-JUN-19' with HR status 'A'. ( in this case also query should fetch only one for 01-JUN-2019 to 03-JUN-19 ) but query is fetching from '01-Jun-19' to '03-Jun-19' one row, and '02-Jun-19' to '03-Jun-19'.
For employee 733931 i am inserted one more with effdt '28-JUN-19' and HR status is Active. in this case also query should fetch one row from 26-JUN-19 to 30-Jun-19, But query is fetching from 26-Jun-19 to 28-Jun-19.
New Insert Statements: ( newly added rows for existing data, same statements included in question as well )
Insert into PS_CTS_JOB_DTL (EMPLID,BUSINESS_UNIT,EMPL_RCD,EFFDT,EFFSEQ,ACTION) values ('733930','US410',0,to_date('02-JUN-19','DD-MON-YY'),0,'DTA');
Insert into PS_CTS_JOB_DTL (EMPLID,BUSINESS_UNIT,EMPL_RCD,EFFDT,EFFSEQ,ACTION) values ('733931','IN208',3,to_date('28-JUN-19','DD-MON-YY'),0,'DTA');
Insert into PS_CTS_JOB_DTL1 (EMPLID,EMPL_RCD,EFFDT,EFFSEQ,HR_STATUS,PER_ORG) values ('733930',0,to_date('02-JUN-19','DD-MON-YY'),0,'A','CWR');
Insert into PS_CTS_JOB_DTL1 (EMPLID,EMPL_RCD,EFFDT,EFFSEQ,HR_STATUS,PER_ORG) values ('733931',3,to_date('28-JUN-19','DD-MON-YY'),0,'A','CWR');
Out put from the System :
Expected Output :
EMPLID FROM_DATE To_DATE PER_ORG HR_STATUS Reason 733930 1-Jun-19 3-Jun-19 CWR A Employee is active as CWR and BU is in IQN_BU setup table 733930 15-Jun-19 18-Jun-19 CWR A Employee is active as CWR and BU is in IQN_BU setup table 733930 26-Jun-19 30-Jun-19 CWR A Employee is active as CWR and BU is in IQN_BU setup table 733931 6-Jun-19 9-Jun-19 CWR A Employee is active as CWR and BU is in IQN_BU setup table 733931 26-Jun-19 30-JUN-19 CWR A Employee is active as CWR and BU is in IQN_BU setup table -
Hi,
Vemula Muni wrote:Hi Frank Kulash, According to above data employee 733930 is active from 01-JUN-2019 to 03-JUN-19. after that i am inserted one more row with effdt '02-JUN-19' with HR status 'A'. ( in this case also query should fetch only one for 01-JUN-2019 to 03-JUN-19 ) but query is fetching from '01-Jun-19' to '03-Jun-19' one row, and '02-Jun-19' to '03-Jun-19'.For employee 733931 i am inserted one more with effdt '28-JUN-19' and HR status is Active. in this case also query should fetch one row from 26-JUN-19 to 30-Jun-19, But query is fetching from 26-Jun-19 to 28-Jun-19.New Insert Statements: ( newly added rows for existing data, same statements included in question as well )Insert into PS_CTS_JOB_DTL (EMPLID,BUSINESS_UNIT,EMPL_RCD,EFFDT,EFFSEQ,ACTION) values ('733930','US410',0,to_date('02-JUN-19','DD-MON-YY'),0,'DTA'); Insert into PS_CTS_JOB_DTL (EMPLID,BUSINESS_UNIT,EMPL_RCD,EFFDT,EFFSEQ,ACTION) values ('733931','IN208',3,to_date('28-JUN-19','DD-MON-YY'),0,'DTA'); Insert into PS_CTS_JOB_DTL1 (EMPLID,EMPL_RCD,EFFDT,EFFSEQ,HR_STATUS,PER_ORG) values ('733930',0,to_date('02-JUN-19','DD-MON-YY'),0,'A','CWR'); Insert into PS_CTS_JOB_DTL1 (EMPLID,EMPL_RCD,EFFDT,EFFSEQ,HR_STATUS,PER_ORG) values ('733931',3,to_date('28-JUN-19','DD-MON-YY'),0,'A','CWR'); Out put from the System :
Expected Output :
EMPLID FROM_DATE To_DATE PER_ORG HR_STATUS Reason 733930 1-Jun-19 3-Jun-19 CWR A Employee is active as CWR and BU is in IQN_BU setup table 733930 15-Jun-19 18-Jun-19 CWR A Employee is active as CWR and BU is in IQN_BU setup table 733930 26-Jun-19 30-Jun-19 CWR A Employee is active as CWR and BU is in IQN_BU setup table 733931 6-Jun-19 9-Jun-19 CWR A Employee is active as CWR and BU is in IQN_BU setup table 733931 26-Jun-19 30-JUN-19 CWR A Employee is active as CWR and BU is in IQN_BU setup table How was anyone supposed to know this from reading your original message? Don't wat until reply #7 to give your requirements: say exactly what you want from the beginning.
I made a guess earlier. I guessed wrong, and wasted lot of time. Don't make people guess. Describe exactly what you want (including what each row in the result set represents) and how you get the desired results from your sample data. Include examples of any special cases (such multiple consecutive rows with hr_status='A') in your sample data, results and explanation.
-
Hi @Frank Kulash, Sorry for giving trouble. there is a chance to have an employee multiple consecutive rows with hr_status='A' when his Data ,location ,assignment and pay scale changed.
-
Hi @Frank Kulash , Please take me out from this issue. Due to this issue my development got struck.