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. 2805438 fiddles created (40855 in the last week).

CREATE TABLE tab ( id INT, col_A TEXT, col_B TEXT, col_C TEXT );
 hidden batch(es)


INSERT INTO tab (id, col_A, col_B, col_C) VALUES (1, 'A1', 'B1', null), (2, 'A2', 'B3', null), (3, 'A3', 'B2', null), (4, 'A5', null, 'C1'), (5, 'A1', null, null);
5 rows affected
 hidden batch(es)


WITH MaxNonNullRowIds AS ( SELECT DISTINCT FIRST_VALUE(col_A) OVER (ORDER BY CASE WHEN col_A IS NULL THEN 0 ELSE id END DESC) AS LastA, FIRST_VALUE(col_B) OVER (ORDER BY CASE WHEN col_B IS NULL THEN 0 ELSE id END DESC) AS LastB, FIRST_VALUE(col_C) OVER (ORDER BY CASE WHEN col_C IS NULL THEN 0 ELSE id END DESC) AS LastC FROM tab ) SELECT LastA, LastB, LastC FROM MaxNonNullRowIds
lasta lastb lastc
A1 B2 C1
 hidden batch(es)