Per i dati di esempio, funziona anche quanto segue
WITH
RawData AS (
SELECT
id1
, jobID
, message
, TIMESTAMP_for_start_message timeStamp
, time_in_seconds
FROM StartMessageTable
UNION ALL
SELECT
id1
, jobID
, message
, TIMESTAMP_success_msg_received
, time_in_seconds
FROM SuccessMessageTable
ORDER BY id1, jobID, 4
),
Detail AS (
SELECT
id1
, jobID
, message message1
, LEAD (message, 1) OVER (ORDER BY id1, jobID, timeStamp) message2
, timeStamp timeStamp1
, LEAD (timeStamp, 1) OVER (ORDER BY id1, jobID, timeStamp) timeStamp2
, time_in_seconds secs1
, LEAD (time_in_seconds, 1) OVER (ORDER BY id1, jobID, timeStamp) secs2
FROM RawData
)
SELECT
id1
, timeStamp1 TIMESTAMP_for_start_message
, timeStamp2 TIMESTAMP_for_success_message
, jobID
, 'start' msgStart
, 'success' msgSuccess
, secs1 start_secs
, secs2 success_secs
FROM Detail
WHERE (message1 = 'start' AND message2 = 'successful') OR message2 IS NULL
ORDER BY timeStamp1
;
con lo stesso avvertimento per quanto riguarda il primo e l'ultimo valore nella colonna "start_secs".
SQL Violino