I am working with a dynamic dataset that needs to be broken up and inserted based on a series of triggers. Dataset Example
In the example, the trigger is the changeover column on the right. What i would like to do is add an additional column that i can group this information by the timestamp value between the first entry that came in, the changeovers, and the last entry.
So far i've organized the project into CTE's to build those triggers and get this far. but i am running out of ideas on how to utlize CTE's to push this any further. Local resources are telling me i need a while loop, these resources are also not used to utilizing CTE's for their data so what i am doing is sort of foreign to them.
looks like a recursive CTE might help here, I am new to these and have been playing around with it for the past few days.
here is the direction i've been heading with the code:
Recursive_CTE(StemID,MasterStem,DateTimeStamp,changeover,InsertID)
as
(
SELECT
StemID
,MasterStem
,changeover
,datetimestamp
,0 as InsertID
FROM CHANGEOVER_CTE
WHERE Masterstem is null
Union ALL
SELECT
ccte.StemID
,ccte.MasterStem
,ccte.DateTimeStamp
,ccte.changeover
,case when (ccte.datetimestamp = rc.changeover)
then
rc.insertID + 1
Else
rc.InsertID
end
FROM CHANGEOVER_CTE as ccte
inner join Recursive_CTE as rc on ccte.datetimestamp = rc.changeover
and the results
problem is, once I hit the first NULL value, the passes of the CTE stop, what i'd like to do is repeat the sequence until the next trigger is hit, then increment that sequence and repeat.
Is CTE recursion the right direction here? i'm probably writing it incorrectly (i've re-written it several dozen times at this point)
any suggestions on how i can solve this dilemma?