我有下表:
DECLARE @TempTable TABLE
(
ID int,
Lastname nvarchar(50),
Firstname nvarchar(50),
Age int
)
insert into @TempTable (ID, Lastname, Firstname, Age)
VALUES(1, 'Smith', NULL, NULL),
(2, NULL, 'Will', 40),
(3, NULL, 'Jaden', 20),
(4, 'Matt', NULL, NULL),
(5, NULL, 'Damon', 39)
我要使用“ 更新”更改表,如下所示:
我知道函数“滞后”和“前导”。但是据我了解,它们不能用于 UPDATE 表达式
答案 0 :(得分:2)
select id ,
(
select Lastname from @TempTable c where c.ID < p.id AND c.Lastname is not null group by Lastname
order by max(id) desc OFFSET 0 ROWS FETCH NEXT 1 ROWS ONLY
) as Lastname,
FirstName,
Age
from @TempTable p
where Firstname is not null;
答案 1 :(得分:1)