SQL to get next not null value in column
问题 How can I get next not null value in column? I have MSSQL 2012 and table with only one column. Like this: rownum Orig ------ ---- 1 NULL 2 NULL 3 9 4 NULL 5 7 6 4 7 NULL 8 9 and I need this data: Rownum Orig New ------ ---- ---- 1 NULL 9 2 NULL 9 3 9 9 4 NULL 7 5 7 7 6 4 4 7 NULL 5 8 9 5 Code to start: declare @t table (rownum int, orig int); insert into @t values (1,NULL),(2,NULL),(3,9),(4,NULL),(5,7),(6,4),(7,NULL),(8,9); select rownum, orig from @t; 回答1: One method is to use outer apply :