I am stuck with this issue. Please refer to screenshot too for table structure.
So we have 2 tables : HISTORY table keeps histiry of payrate, location etc & POSITION table only gives details of Job action changes datewise. Desired output we want to only consider Action_code as 'JOB-CHANGE' and get the latest payrate and previous payrate before the action- job change.
--- table1: HISTORY ---
| EMPLOYEE | EFFECT_DATE | END_DATE | PAY_RATE | LOCAT_CODE |
|---|---|---|---|---|
| 344 | 4/1/2021 | current - TBD | 42.44 | ATL |
| 344 | 3/2/2021 | 3/31/2021 | 41.81 | ATL |
| 344 | 3/31/2020 | 3/1/2021 | 41.81 | DTW |
--- table2 ACTION ---
| COMPANY | EMPLOYEE | EFFECT_DATE | ACTION_CODE | REASON_01 |
|---|---|---|---|---|
| 100 | 344 | 3/31/2021 | LOA | ST-RTW |
| 100 | 344 | 3/2/2021 | JOB CHANGE | JC-TRANS(L |
| 100 | 344 | 5/31/2020 | LOA | ST-COVID90 |
--- DESIRED OUTPUT --
| Employee | Action code | Reason_1 | Effective_Date | Previous_Location | Previous_Rate | New_Location | New_Pay_Rate |
|---|---|---|---|---|---|---|---|
| 344 | JOB CHANGE | JC-TRANS(L | 3/2/2021 | DTW | 41.81 | ATL | 42.44 |
Could you please help me out here. Thanks much :) in advance
LEAD/LAG. The difficult bit is how do you want to match the rows. The only common columns I see areEMPLOYEE, EFFECT_DATE. If you match on those two, is the date with a time component, then do we do an exact match? Or do we get the first row on or before that date from thehistorytable? And how isNew_Pay_Rate: 42.44worked out, I don't get the logic