clear markdown compare help best fiddles feedback dbanow.uk
clear markdown donate comments/suggestions/bugs a leap of faith? bible365 diddy dollings
By using db<>fiddle, you agree to license everything you submit by Creative Commons CC0. 2805483 fiddles created (40776 in the last week).

WITH table1 AS ( SELECT 1084465 AS Id, 11802 AS PersonId, '2019-01-18 15:45:44.000' AS ProcessStepOne UNION ALL SELECT 1084507, 11802, '2019-01-18 16:07:22.000' ), table2 AS ( SELECT 1016970 AS Id, 11802 AS PersonId, '2019-01-24 12:51:52.600' AS ProcessStepTwo UNION ALL SELECT 1016996, 11802, '2019-01-24 12:55:21.953' ), cte AS ( SELECT Id, PersonId, ProcessStepOne AS ProcessStep, 1 AS source, ROW_NUMBER() OVER (PARTITION BY PersonID ORDER BY ProcessStepOne) rn FROM table1 UNION ALL SELECT Id, PersonId, ProcessStepTwo, 2, ROW_NUMBER() OVER (PARTITION BY PersonID ORDER BY ProcessStepTwo) FROM table2 ) SELECT MAX(CASE WHEN source = 1 THEN Id END) AS Id, PersonId, MIN(ProcessStep) AS ProcessStepOne, MAX(ProcessStep) AS ProcessStepTwo FROM cte GROUP BY PersonId, rn;
Id PersonId ProcessStepOne ProcessStepTwo
1084465 11802 2019-01-18 15:45:44.000 2019-01-24 12:51:52.600
1084507 11802 2019-01-18 16:07:22.000 2019-01-24 12:55:21.953
Warning: Null value is eliminated by an aggregate or other SET operation.
 hidden batch(es)