We have got a table with a identifier, a key/value pairs and a start and end timestamp which indicates the valid period for the values.
| MASTER_WORK_ORDR_ID | START_TS | END_TS | WORK_ORDR_ID_CTXT | WORK_ORDR_NUM |
|---|---|---|---|---|
| 955.244.397 | 02/06/2025 12:27:38.882743 | 31/12/9999 23:59:59.999999 | WFM | NPA 25060213905819 |
| 955.244.397 | 02/06/2025 12:27:38.882743 | 14/06/2025 17:08:09.407642 | CALLID | NPA 25060213905819 |
| 955.244.397 | 02/06/2025 12:27:38.882743 | 14/06/2025 17:08:09.407642 | WFM_INTERNAL | 714372625 |
| 955.244.397 | 02/06/2025 12:27:38.882743 | 02/06/2025 12:31:21.200409 | NPA | 14743010 |
| 955.244.397 | 02/06/2025 12:31:21.200410 | 31/12/9999 23:59:59.999999 | NPA | 268556282 |
| 955.244.397 | 14/06/2025 17:08:09.407643 | 14/06/2025 17:08:09.772443 | CALLID | NPA 25060213905819-1 |
| 955.244.397 | 14/06/2025 17:08:09.407643 | 14/06/2025 17:08:09.772443 | WFM_INTERNAL | 444207219 |
| 955.244.397 | 14/06/2025 17:08:09.772444 | 14/06/2025 17:14:52.617407 | WFM_INTERNAL | 714372625 |
| 955.244.397 | 14/06/2025 17:08:09.772444 | 14/06/2025 17:14:52.617407 | CALLID | NPA 25060213905819 |
| 955.244.397 | 14/06/2025 17:14:52.617408 | 16/06/2025 10:05:43.590662 | WFM_INTERNAL | 444207219 |
| 955.244.397 | 14/06/2025 17:14:52.617408 | 16/06/2025 10:05:43.590662 | CALLID | NPA 25060213905819-1 |
| 955.244.397 | 16/06/2025 10:05:43.590663 | 16/06/2025 10:05:44.111454 | CALLID | NPA 25060213905819-2 |
| 955.244.397 | 16/06/2025 10:05:43.590663 | 16/06/2025 10:05:44.111454 | WFM_INTERNAL | 575199896 |
| 955.244.397 | 16/06/2025 10:05:44.111455 | 16/06/2025 10:07:21.934714 | WFM_INTERNAL | 444207219 |
| 955.244.397 | 16/06/2025 10:05:44.111455 | 16/06/2025 10:07:21.934714 | CALLID | NPA 25060213905819-1 |
| 955.244.397 | 16/06/2025 10:07:21.934715 | 31/12/9999 23:59:59.999999 | WFM_INTERNAL | 575199896 |
| 955.244.397 | 16/06/2025 10:07:21.934715 | 31/12/9999 23:59:59.999999 | CALLID | NPA 25060213905819-2 |
I pivot this to get a more readable format
SELECT
MASTER_WORK_ORDR_ID WORK_ORDR_ID
,START_TS
,END_TS
,"'UTS'_VAL" UTS
,"'SER'_VAL" SER
,"'SPRINT'_VAL" SPRINT
,"'LDB'_VAL" LDB
,"'NPA'_VAL" NPA
,"'WFM_INTERNAL'_VAL" WFM_INTERNAL
,"'INTERNAL'_VAL" INTERNAL
,"'ABDTBR'_VAL" ABDTBR
,"'WFMIL'_VAL" WFMIL
,"'WFM'_VAL" WFM
,"'CALLID'_VAL" CALLID
FROM
(
SELECT
MASTER_WORK_ORDR_ID
,START_TS
,END_TS
,WORK_ORDR_ID_CTXT
,WORK_ORDR_NUM
FROM P0_EDV1_ATO.WORK_ORDR_IDENTIFIER_HIST
WHERE 1=1
AND MASTER_WORK_ORDR_ID = 955244397
) s_tab
PIVOT
(
Max(WORK_ORDR_NUM) val
FOR WORK_ORDR_ID_CTXT IN
(
'UTS'
,'SER'
,'SPRINT'
,'LDB'
,'NPA'
,'WFM_INTERNAL'
,'INTERNAL'
,'ABDTBR'
,'WFMIL'
,'WFM'
,'CALLID'
)
) p_tab
ORDER BY 1,2,3 DESC;
This gets me to the following
| WORK_ORDR_ID | START_TS | END_TS | UTS | SER | SPRINT | LDB | NPA | WFM_INTERNAL | INTERNAL | ABDTBR | WFMIL | WFM | CALLID |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 955.244.397 | 02/06/2025 12:27:38.882743 | 31/12/9999 23:59:59.999999 | NPA 25060213905819 | ||||||||||
| 955.244.397 | 02/06/2025 12:27:38.882743 | 02/06/2025 12:31:21.200409 | 14743010 | ||||||||||
| 955.244.397 | 02/06/2025 12:27:38.882743 | 14/06/2025 17:08:09.407642 | 714372625 | NPA 25060213905819 | |||||||||
| 955.244.397 | 02/06/2025 12:31:21.200410 | 31/12/9999 23:59:59.999999 | 268556282 | ||||||||||
| 955.244.397 | 14/06/2025 17:08:09.407643 | 14/06/2025 17:08:09.772443 | 444207219 | NPA 25060213905819-1 | |||||||||
| 955.244.397 | 14/06/2025 17:08:09.772444 | 14/06/2025 17:14:52.617407 | 714372625 | NPA 25060213905819 | |||||||||
| 955.244.397 | 14/06/2025 17:14:52.617408 | 16/06/2025 10:05:43.590662 | 444207219 | NPA 25060213905819-1 | |||||||||
| 955.244.397 | 16/06/2025 10:05:43.590663 | 16/06/2025 10:05:44.111454 | 575199896 | NPA 25060213905819-2 | |||||||||
| 955.244.397 | 16/06/2025 10:05:44.111455 | 16/06/2025 10:07:21.934714 | 444207219 | NPA 25060213905819-1 | |||||||||
| 955.244.397 | 16/06/2025 10:07:21.934715 | 31/12/9999 23:59:59.999999 | 575199896 | NPA 25060213905819-2 |
But this does not get me what I really need. In fact I need, on each line, all values which are valid for that period of time indicated by start and end ts. So, the ones that changed and which are valid as from that start_ts but also the ones which are still valid from above since the end_ts is higher than the end_ts of the current record.
The end result should be
- row 1 has 1 value (can be more) which is valid to the end of time so this should be propagated to all records.
- row 2 throws in another value which is only valid till 02/06/2025 12:31:21 but the value of row 3 is also valid in that timeframe.
- row 4 changes the value from row 2 and it becomes valid from that time on until eternity so it must be propagated to all records underneath.
- etc.
Values must be merged together as long as they are valid, that's the bottom line.
It will get me this
| WORK_ORDR_ID | START_TS | END_TS | UTS | SER | SPRINT | LDB | NPA | WFM_INTERNAL | INTERNAL | ABDTBR | WFMIL | WFM | CALLID |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 955.244.397 | 02/06/2025 12:27:38.882743 | 31/12/9999 23:59:59.999999 | NPA 25060213905819 | ||||||||||
| 955.244.397 | 02/06/2025 12:27:38.882743 | 02/06/2025 12:31:21.200409 | 14743010 | 714372625 | NPA 25060213905819 | ||||||||
| 955.244.397 | 02/06/2025 12:27:38.882743 | 14/06/2025 17:08:09.407642 | 714372625 | NPA 25060213905819 | NPA 25060213905819 | ||||||||
| 955.244.397 | 02/06/2025 12:31:21.200410 | 31/12/9999 23:59:59.999999 | 268556282 | 714372625 | NPA 25060213905819 | ||||||||
| 955.244.397 | 14/06/2025 17:08:09.407643 | 14/06/2025 17:08:09.772443 | 268556282 | 444207219 | NPA 25060213905819 | NPA 25060213905819-1 | |||||||
| 955.244.397 | 14/06/2025 17:08:09.772444 | 14/06/2025 17:14:52.617407 | 268556282 | 714372625 | NPA 25060213905819 | NPA 25060213905819 | |||||||
| 955.244.397 | 14/06/2025 17:14:52.617408 | 16/06/2025 10:05:43.590662 | 268556282 | 444207219 | NPA 25060213905819 | NPA 25060213905819-1 | |||||||
| 955.244.397 | 16/06/2025 10:05:43.590663 | 16/06/2025 10:05:44.111454 | 268556282 | 575199896 | NPA 25060213905819 | NPA 25060213905819-2 | |||||||
| 955.244.397 | 16/06/2025 10:05:44.111455 | 16/06/2025 10:07:21.934714 | 268556282 | 444207219 | NPA 25060213905819 | NPA 25060213905819-1 | |||||||
| 955.244.397 | 16/06/2025 10:07:21.934715 | 31/12/9999 23:59:59.999999 | 268556282 | 575199896 | NPA 25060213905819 | NPA 25060213905819-2 |
The next step is grouping on all values and taking the min and max timestamps to get the outer timeframe in which values are valid. You'll notice that there is an overlap. This is due to the fact that a new version of a work order is created before the old one is completed.
As final result we should get
| WORK_ORDR_ID | START_TS | END_TS | UTS | SER | SPRINT | LDB | NPA | WFM_INTERNAL | INTERNAL | ABDTBR | WFMIL | WFM | CALLID |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 955.244.397 | 02/06/2025 12:27:38.882743 | 02/06/2025 12:31:21.200409 | 14743010 | 714372625 | NPA 25060213905819 | ||||||||
| 955.244.397 | 02/06/2025 12:27:38.882743 | 14/06/2025 17:14:52.617407 | 268556282 | 714372625 | NPA 25060213905819 | NPA 25060213905819 | |||||||
| 955.244.397 | 14/06/2025 17:08:09.407643 | 16/06/2025 10:07:21.934714 | 268556282 | 444207219 | NPA 25060213905819 | NPA 25060213905819-1 | |||||||
| 955.244.397 | 16/06/2025 10:05:43.590663 | 31/12/9999 23:59:59.999999 | 268556282 | 575199896 | NPA 25060213905819 | NPA 25060213905819-2 |