sql - LAG functions and NULLS -
how can tell lag function last "not null" value?
for example, see table bellow have few null values on column b , c. i'd fill nulls last non-null value. tried using lag function, so:
case when b null lag (b) on (order idx) else b end b,
but doesn't quite work when have 2 or more nulls in row (see null value on column c row 3 - i'd 0.50 original).
any idea how can achieve that? (it doesn't have using lag function, other ideas welcome)
a few assumptions:
- the number of rows dynamic;
- the first value non-null;
- once have null, null end - want fill latest value.
thanks
if null way end can take short cut
declare @b varchar(20) = (select top 1 b table b not null order id desc); declare @c varchar(20) = (select top 1 c table c not null order id desc); select is, isnull(b,@b) b, insull(c,@c) c table;
Comments
Post a Comment