drop table t
go
create table t (id int, name varchar(10), organisation varchar(10))
go
truncate table t
insert into t values
(1,'n1','o1'),(2,null,'o2'),(3, null, 'o3'),(4,'n2','o4'),(5,null,'o5')
Чтобы перезаписать нулевые имена предыдущим ненулевым именем, а затем использовать коррелированный подзапрос, чтобы найти предыдущее имя, будет выглядеть так:
select t.id,
(select t2.name from t t2 where t2.name is not null and t2.id = (select max(id) from t t3 where t3.name is not null and t3.id < t.id)) name,
t.organisation
from t
where name is null
result
id name organisation
----------- ---------- ------------
2 n1 o2
3 n1 o3
5 n2 o5
(3 row(s) affected)
И наоборот, чтобы найти следующую организацию
select t.id,t.name,
(select t2.organisation from t t2 where t2.name is null and t2.id = (select min(id) from t t3 where t3.name is null and t3.id > t.id)) organisation
from t
where name is not null
Result
id name organisation
----------- ---------- ------------
1 n1 o2
4 n2 o5
(2 row(s) affected)
ОБА результаты соответствуют вашим требованиям (и, конечно, совершенно разные)