By using db<>fiddle, you agree to license everything you submit by Creative Commons CC0.
WITH CTE AS (SELECT 1 ID, null STATUS,0 "MODE", '13:46' UPDATE_DATE from dual UNION ALL
SELECT 1,null,1,'12:22' from dual UNION ALL
SELECT 1,1,null,'15:00' from dual UNION ALL
SELECT 1,0,null,'15:55' from dual UNION ALL
SELECT 2 ID, null STATUS,0 "MODE", '13:46' UPDATE_DATE from dual UNION ALL
SELECT 2,null,1,'12:22' from dual UNION ALL
SELECT 2,0,null,'15:00' from dual UNION ALL
SELECT 2,1,null,'15:55' from dual UNION ALL
SELECT 3 ID, null STATUS,1 "MODE", '13:46' UPDATE_DATE from dual UNION ALL
SELECT 3,null,0,'12:22' from dual UNION ALL
SELECT 3,1,null,'15:00' from dual UNION ALL
SELECT 3,0,null,'15:55' from dual)
SELECT A.ID, Z.Status, Y."MODE", max(A.Update_Date) UD
FROM CTE A
CROSS APPLY (SELECT B.Status FROM CTE B WHERE B.ID = A.ID and B.Status is not null ORDER BY UPDATE_DATE DESC FETCH FIRST 1 ROW ONLY) Z
CROSS APPLY (SELECT B."MODE" FROM CTE B WHERE B.ID = A.ID and B."MODE" is not null ORDER BY UPDATE_DATE DESC FETCH FIRST 1 ROW ONLY) Y
GROUP BY ID, Z.Status, Y."MODE"
ID | STATUS | MODE | UD |
---|---|---|---|
3 | 0 | 1 | 15:55 |
1 | 0 | 0 | 15:55 |
2 | 1 | 0 | 15:55 |