列を使用して順序を決定すると仮定するとID
、ループなしで実行できます。
あなたがこれをしたいかどうかは別の問題です - それはきれいに見えません:
declare @t table (ID int, Date date, D1 int, D2 char(1), D3 char(1))
insert into @t(ID, Date, D1, D2, D3) values
(1,'20130101',0,'X','A'),
(2,'20130201',null,null,null),
(3,'20130301',1,null,null),
(4,'20130401',null,null,'B'),
(5,'20130501',null,null,null)
update a
set
a.D1 = COALESCE(a.D1,d1.D1),
a.D2 = COALESCE(a.D2,d2.D2),
a.D3 = COALESCE(a.D3,d3.D3)
from
@t a
left join
@t D1
on
D1.ID < a.ID and
D1.D1 IS NOT NULL
left join
@t D1_anti
on
D1_anti.ID < a.ID and
D1_anti.D1 is not null and
D1_anti.ID > D1.ID
left join
@t D2
on
D2.ID < a.ID and
D2.D2 IS NOT NULL
left join
@t D2_anti
on
D2_anti.ID < a.ID and
D2_anti.D2 is not null and
D2_anti.ID > D2.ID
left join
@t D3
on
D3.ID < a.ID and
D3.D3 IS NOT NULL
left join
@t D3_anti
on
D3_anti.ID < a.ID and
D3_anti.D3 is not null and
D3_anti.ID > D3.ID
where
D1_anti.ID is null and
D2_anti.ID is null and
D3_anti.ID is null
select * from @t
基本的に、結合を実行して適用可能な前の行を見つけようとし、_anti
結合を実行して、前に見つかった各行が存在する最新の行であることを確認します。