0

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
10
  • 1
    Why implement such complex logic in SQL? This can be done much simpler using EF or reporting tools. Commented Nov 19 at 15:33
  • 1
    Where in the original data are the overlaps? Commented Nov 19 at 15:46
  • for line 2 the timeframe is from 02/06/2025 12:27:38.882743 till 14/06/2025 17:14:52.617407 for line 3 the timeframe is from 14/06/2025 17:08:09.407643 till 16/06/2025 10:07:21.934714 so an overlap^ there are other tables with the work_ordr_id and a timestamp which I must link to this, so an overlap doe not make that eassier, although I have a workaround for that. for now I just need to get all the proper values together in 1 line per different version of the CALLID Commented Nov 20 at 8:13
  • why in sql, well sometimes you just got todo what you got todo with the tools provided Commented Nov 20 at 8:14
  • That's an overlap in your expected results. Where in the original data is the overlap? Commented Nov 20 at 12:10

2 Answers 2

1

Each change in any parameter generates a new series of values ("dataSeries") for all parameters.
If several parameters are changed at the same time, we combine them into one series.

We take distinct values of "start_ts" for every "master_work_ordr_id".
This "dataSeries" is actual up to next change. Calculate this "up_to_ts" as next_ts-1 mcs like

  dateadd(mcs,-1,lead(start_ts,1,'9999-12-31 23:59:59.999999')
          over(partition by master_work_ordr_id order by start_ts)) up_to_ts

We are creating such a "framework" of the report.

 select master_work_ordr_id,start_ts 
        ,dateadd(mcs,-1,lead(start_ts,1,'9999-12-31 23:59:59.999999')
          over(partition by master_work_ordr_id order by start_ts)) up_to_ts
 from (select distinct master_work_ordr_id,start_ts from WORK_ORDR_IDENTIFIER_HIST)x
master_work_ordr_id start_ts up_to_ts
955.244.397 2025-06-02 12:27:38.8827430 2025-06-02 12:31:21.2004090
955.244.397 2025-06-02 12:31:21.2004100 2025-06-14 17:08:09.4076420
955.244.397 2025-06-14 17:08:09.4076430 2025-06-14 17:08:09.7724430
955.244.397 2025-06-14 17:08:09.7724440 2025-06-14 17:14:52.6174070
955.244.397 2025-06-14 17:14:52.6174080 2025-06-16 10:05:43.5906620
955.244.397 2025-06-16 10:05:43.5906630 2025-06-16 10:05:44.1114540
955.244.397 2025-06-16 10:05:44.1114550 2025-06-16 10:07:21.9347140
955.244.397 2025-06-16 10:07:21.9347150 9999-12-31 23:59:59.9999980

Then, for each "dataSeries", we get the current value of all the parameters. To do this, attach (LEFT JOIN) the "WORK_ORDR_IDENTIFIER_HIST" table to the framework.

For source data

MASTER_WORK_ORDR_ID START_TS END_TS WORK_ORDR_ID_CTXT WORK_ORDR_NUM
955.244.397 2025-06-02 12:27:38.8827430 9999-12-31 23:59:59.9999990 WFM NPA 25060213905819
955.244.397 2025-06-02 12:27:38.8827430 2025-06-14 17:08:09.4076420 CALLID NPA 25060213905819
955.244.397 2025-06-02 12:27:38.8827430 2025-06-14 17:08:09.4076420 WFM_INTERNAL 714372625
955.244.397 2025-06-02 12:27:38.8827430 2025-06-02 12:31:21.2004090 NPA 14743010
955.244.397 2025-06-02 12:31:21.2004100 9999-12-31 23:59:59.9999990 NPA 268556282
955.244.397 2025-06-14 17:08:09.4076430 2025-06-14 17:08:09.7724430 CALLID NPA 25060213905819-1
955.244.397 2025-06-14 17:08:09.4076430 2025-06-14 17:08:09.7724430 WFM_INTERNAL 444207219
955.244.397 2025-06-14 17:08:09.7724440 2025-06-14 17:14:52.6174070 WFM_INTERNAL 714372625
955.244.397 2025-06-14 17:08:09.7724440 2025-06-14 17:14:52.6174070 CALLID NPA 25060213905819
955.244.397 2025-06-14 17:14:52.6174080 2025-06-16 10:05:43.5906620 WFM_INTERNAL 444207219
955.244.397 2025-06-14 17:14:52.6174080 2025-06-16 10:05:43.5906620 CALLID NPA 25060213905819-1
955.244.397 2025-06-16 10:05:43.5906630 2025-06-16 10:05:44.1114540 CALLID NPA 25060213905819-2
955.244.397 2025-06-16 10:05:43.5906630 2025-06-16 10:05:44.1114540 WFM_INTERNAL 575199896
955.244.397 2025-06-16 10:05:44.1114550 2025-06-16 10:07:21.9347140 WFM_INTERNAL 444207219
955.244.397 2025-06-16 10:05:44.1114550 2025-06-16 10:07:21.9347140 CALLID NPA 25060213905819-1
955.244.397 2025-06-16 10:07:21.9347150 9999-12-31 23:59:59.9999990 WFM_INTERNAL 575199896
955.244.397 2025-06-16 10:07:21.9347150 9999-12-31 23:59:59.9999990 CALLID NPA 25060213905819-2

Finally, PIVOT data

with dataSeries as(
  select master_work_ordr_id,start_ts 
        ,dateadd(mcs,-1,lead(start_ts,1,'9999-12-31 23:59:59.999999')
          over(partition by master_work_ordr_id order by start_ts)) up_to_ts
  from (
       select distinct master_work_ordr_id,start_ts 
       from WORK_ORDR_IDENTIFIER_HIST
       where MASTER_WORK_ORDR_ID = '955.244.397'
      )x
)
, expData as(
  select  ds.master_work_ordr_id,ds.start_ts,ds.up_to_ts,t.end_ts
    ,t.WORK_ORDR_ID_CTXT, t.WORK_ORDR_NUM
  from dataSeries ds
  left join WORK_ORDR_IDENTIFIER_HIST t on ds.master_work_ordr_id=t.master_work_ordr_id
  and ds.start_ts between t.start_ts and t.end_ts 
  and t.MASTER_WORK_ORDR_ID = '955.244.397'
)
SELECT MASTER_WORK_ORDR_ID WORK_ORDR_ID 
    ,START_TS,up_to_ts  -- ,"UTS" UTS ,"SER" SER ,"SPRINT" SPRINT ,"LDB" LDB 
    ,"NPA" NPA ,"WFM_INTERNAL" WFM_INTERNAL -- ,"INTERNAL" INTERNAL ,"ABDTBR" ABDTBR ,"WFMIL" WFMIL 
    ,"WFM" WFM 
    ,"CALLID" CALLID 
FROM 
    (
    SELECT  MASTER_WORK_ORDR_ID ,START_TS,up_to_ts ,WORK_ORDR_ID_CTXT ,WORK_ORDR_NUM
    FROM expData 
--    WHERE 1=1 AND MASTER_WORK_ORDR_ID = '955.244.397' 
    )   s_tab 
    PIVOT   
    (
        max(WORK_ORDR_NUM) 
        FOR WORK_ORDR_ID_CTXT IN
            ( -- "UTS" ,"SER" ,"SPRINT" ,"LDB"
                 "NPA" ,"WFM_INTERNAL"  --    ,"INTERNAL" ,"ABDTBR" ,"WFMIL"
                ,"WFM" ,"CALLID" 
            ) 
    ) p_tab 
ORDER BY MASTER_WORK_ORDR_ID, START_TS;     
;
WORK_ORDR_ID START_TS up_to_ts NPA WFM_INTERNAL WFM CALLID
955.244.397 2025-06-02 12:27:38.8827430 2025-06-02 12:31:21.2004090 14743010 714372625 NPA 25060213905819 NPA 25060213905819
955.244.397 2025-06-02 12:31:21.2004100 2025-06-14 17:08:09.4076420 268556282 714372625 NPA 25060213905819 NPA 25060213905819
955.244.397 2025-06-14 17:08:09.4076430 2025-06-14 17:08:09.7724430 268556282 444207219 NPA 25060213905819 NPA 25060213905819-1
955.244.397 2025-06-14 17:08:09.7724440 2025-06-14 17:14:52.6174070 268556282 714372625 NPA 25060213905819 NPA 25060213905819
955.244.397 2025-06-14 17:14:52.6174080 2025-06-16 10:05:43.5906620 268556282 444207219 NPA 25060213905819 NPA 25060213905819-1
955.244.397 2025-06-16 10:05:43.5906630 2025-06-16 10:05:44.1114540 268556282 575199896 NPA 25060213905819 NPA 25060213905819-2
955.244.397 2025-06-16 10:05:44.1114550 2025-06-16 10:07:21.9347140 268556282 444207219 NPA 25060213905819 NPA 25060213905819-1
955.244.397 2025-06-16 10:07:21.9347150 9999-12-31 23:59:59.9999980 268556282 575199896 NPA 25060213905819 NPA 25060213905819-2

Intermediate result of JOIN dataSeries and WORK_ORDR_IDENTIFIER_HIST see in fiddle.

select *
from(  select distinct master_work_ordr_id,start_ts 
        ,dateadd(mcs,-1,lead(start_ts,1,'9999-12-31 23:59:59.999999')
          over(partition by master_work_ordr_id order by start_ts)) up_to_ts
      from WORK_ORDR_IDENTIFIER_HIST) ds
left join WORK_ORDR_IDENTIFIER_HIST t on ds.master_work_ordr_id=t.master_work_ordr_id
  and ds.start_ts between t.start_ts and t.end_ts
;

Next time (up_to_ts) calculation example

select *
  ,cast(dateadd(mcs,-1,dttm) as datetime2(7)) res
from( select cast('2025-06-02 12:31:21.2004100' as datetime2(7)) dttm)t
dttm res
2025-06-02 12:31:21.2004100 2025-06-02 12:31:21.2004090

For "master_work_ordr_id" data type you use "955.244.397" as string (varchar) and as integer (MASTER_WORK_ORDR_ID = 955244397). I gave the example as varchar. This does not affect the decision.

fiddle

Sign up to request clarification or add additional context in comments.

5 Comments

It should be noted that the LEFT JOIN in expData can't make good use of indexes and results in a triangular join. This is fine for small data sets, but scales poorly. The OP is not clear on the size of the live dataset, so this may or may not be relevant.
Up voted as, like my answer, it tries to highlight that the OP's desired results don't appear to match the question's source data and description.
Thank you for comment. I'll move filter "MASTER_WORK_ORDR_ID = '955.244.397' " up in query. This will improve performance. Perhaps, OP have index on table WORK_ORDR_IDENTIFIER_HIST (WORK_ORDR_ID, START_TS).
It's not that, the position on the ID predicate is fine. It's the nature of the time-range check. Even if the table is indexed on START_TS, the optimiser can't tell that rows don't overlap. This means that every row where t.start_ts >= ds.start_ts has to be checked (thus the triangular join).
Yes, it's worth thinking about.
1

I'm going to assume a few things...

  1. There are actually no overlaps in the source data (there are none in your post)
  2. Therefore there are no overlaps in the output data (so, your post must be mistaken)
  3. There isn't actually a 1µs gap if a row ends at .407642 followed by a row at .407643

As such, my expected results for your sample data would be...

MASTER_WORK_ORDR_ID START_TS END_TS CTXT_CALLID CTXT_NPA CTXT_WFM CTXT_WFM_INTERNAL
955.244.397 2025-06-02 12:27:38.8827430 2025-06-02 12:31:21.2004100 NPA 25060213905819 14743010 NPA 25060213905819 714372625
955.244.397 2025-06-02 12:31:21.2004100 2025-06-14 17:08:09.4076430 NPA 25060213905819 268556282 NPA 25060213905819 714372625
955.244.397 2025-06-14 17:08:09.4076430 2025-06-14 17:08:09.7724440 NPA 25060213905819-1 268556282 NPA 25060213905819 444207219
955.244.397 2025-06-14 17:08:09.7724440 2025-06-14 17:14:52.6174080 NPA 25060213905819 268556282 NPA 25060213905819 714372625
955.244.397 2025-06-14 17:14:52.6174080 2025-06-16 10:05:43.5906630 NPA 25060213905819-1 268556282 NPA 25060213905819 444207219
955.244.397 2025-06-16 10:05:43.5906630 2025-06-16 10:05:44.1114550 NPA 25060213905819-2 268556282 NPA 25060213905819 575199896
955.244.397 2025-06-16 10:05:44.1114550 2025-06-16 10:07:21.9347150 NPA 25060213905819-1 268556282 NPA 25060213905819 444207219
955.244.397 2025-06-16 10:07:21.9347150 null NPA 25060213905819-2 268556282 NPA 25060213905819 575199896

First...Things are significantly easier if time ranges are "inclusive start, exclusive end". That is 12:05:06.001 to 12:06:06.001 would be exactly one minute long.

And, in fact, they're even easier if there are no end times at all (a row is implicitly ended by the existence of a subsequent row).

This does mean that for a value to revert to "blank", an explicit row needs to be added forcing that blank.

For example...

start end value
00:00 11:58.999999 smeeeg
12:01 forever heeeed

Would become...

start value
00:00 smeeeg
11:59
12:01 heeeed

The first part of my code is to do just that, overly carefully...

  • This can be completely skipped if there are no gaps in the data
  • Your example data has no gaps, so this code (and the END_TS column) may be skippable
WITH
  fix_end AS
(
  SELECT
    MASTER_WORK_ORDR_ID,
    START_TS,
    DATEADD(microsecond, 1, NULLIF(END_TS,'9999-12-31 23:59:59.999999'))   AS END_TS,
    WORK_ORDR_ID_CTXT,
    WORK_ORDR_NUM
  FROM
    WORK_ORDR_IDENTIFIER_HIST
),
  unioned_terminations AS
(
  SELECT
    MASTER_WORK_ORDR_ID, START_TS, WORK_ORDR_ID_CTXT, WORK_ORDR_NUM
  FROM
    fix_end
  
  UNION ALL
  
  -- Generate rows where there are gaps in time for any `(MASTER_WORK_ORDR_ID, WORK_ORDR_ID_CTXT)`
  SELECT
    MASTER_WORK_ORDR_ID, END_TS, WORK_ORDR_ID_CTXT, ''
  FROM
    fix_end
  QUALIFY
    END_TS
    <
    LEAD(START_TS)
      OVER (
        PARTITION BY MASTER_WORK_ORDR_ID, WORK_ORDR_ID_CTXT
            ORDER BY START_TS
      )
)
SELECT * FROM unioned_terminations
  ORDER BY MASTER_WORK_ORDR_ID,WORK_ORDR_ID_CTXT,START_TS;
MASTER_WORK_ORDR_ID START_TS WORK_ORDR_ID_CTXT WORK_ORDR_NUM
955.244.397 2025-06-02 12:27:38.8827430 CALLID NPA 25060213905819
955.244.397 2025-06-14 17:08:09.4076430 CALLID NPA 25060213905819-1
955.244.397 2025-06-14 17:08:09.7724440 CALLID NPA 25060213905819
955.244.397 2025-06-14 17:14:52.6174080 CALLID NPA 25060213905819-1
955.244.397 2025-06-16 10:05:43.5906630 CALLID NPA 25060213905819-2
955.244.397 2025-06-16 10:05:44.1114550 CALLID NPA 25060213905819-1
955.244.397 2025-06-16 10:07:21.9347150 CALLID NPA 25060213905819-2
955.244.397 2025-06-02 12:27:38.8827430 NPA 14743010
955.244.397 2025-06-02 12:31:21.2004100 NPA 268556282
955.244.397 2025-06-02 12:27:38.8827430 WFM NPA 25060213905819
955.244.397 2025-06-02 12:27:38.8827430 WFM_INTERNAL 714372625
955.244.397 2025-06-14 17:08:09.4076430 WFM_INTERNAL 444207219
955.244.397 2025-06-14 17:08:09.7724440 WFM_INTERNAL 714372625
955.244.397 2025-06-14 17:14:52.6174080 WFM_INTERNAL 444207219
955.244.397 2025-06-16 10:05:43.5906630 WFM_INTERNAL 575199896
955.244.397 2025-06-16 10:05:44.1114550 WFM_INTERNAL 444207219
955.244.397 2025-06-16 10:07:21.9347150 WFM_INTERNAL 575199896

Once we have the data in that format (no overlaps, no gaps, rows end due to the existence of a following row) the data can then be pivotted, and LAST_VALUE() IGNORE NULLS used to fill the gaps.

SELECT
  MASTER_WORK_ORDR_ID,
  START_TS,
  LEAD(START_TS          )              OVER (PARTITION BY MASTER_WORK_ORDR_ID ORDER BY START_TS)   AS END_TS,
  LAST_VALUE(CALLID      ) IGNORE NULLS OVER (PARTITION BY MASTER_WORK_ORDR_ID ORDER BY START_TS)   AS CTXT_CALLID,
  LAST_VALUE(NPA         ) IGNORE NULLS OVER (PARTITION BY MASTER_WORK_ORDR_ID ORDER BY START_TS)   AS CTXT_NPA,
  LAST_VALUE(WFM         ) IGNORE NULLS OVER (PARTITION BY MASTER_WORK_ORDR_ID ORDER BY START_TS)   AS CTXT_WFM,
  LAST_VALUE(WFM_INTERNAL) IGNORE NULLS OVER (PARTITION BY MASTER_WORK_ORDR_ID ORDER BY START_TS)   AS CTXT_WFM_INTERNAL
FROM
(
  SELECT
    MASTER_WORK_ORDR_ID, START_TS, WORK_ORDR_ID_CTXT, WORK_ORDR_NUM
  FROM
    WORK_ORDR_IDENTIFIER_HIST
)
  AS src
/*
 * Or `unioned_terminations AS src` if there can be gaps in the data
 */
PIVOT   
(
  MAX(WORK_ORDR_NUM) 
    FOR WORK_ORDR_ID_CTXT IN
    (
       "NPA", "WFM_INTERNAL", "WFM", "CALLID" 
    ) 
)
    AS pvt
ORDER BY
  MASTER_WORK_ORDR_ID,
  START_TS
;

demo using SQL SERVER, as I don't know of a demo host for Teradata

1 Comment

thanks for thinking along with me, I'll try the proposed solutions next week and keep you update. always nice to get new visions if you're struggling with a problem

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.