gpt4 book ai didi

SQL Server : Update Query

转载 作者:行者123 更新时间:2023-12-03 02:08:01 25 4
gpt4 key购买 nike

Sequence_ID Dex_ID
1 1
null 2
null 3
2 4
null 5
null 6
3 7
null 8
null 9
4 10
null 11
null 12
3 13

以上是数据集。空值应由两个非空值之间的先前值更新。 equence_id 不一定按特定顺序排列,但 Dex_ID 遵循升序排列。

输出应如下所示:

Sequence_ID Dex_ID
1 1
1 2
1 3
2 4
2 5
2 6
3 7
3 8
3 9
4 10
4 11
4 12
3 13

有什么建议吗?

最佳答案

SQL Fiddle example

UPDATE
f1
SET
Sequence_ID = x.Sequence_ID
FROM
MyTable f1
CROSS APPLY
(SELECT TOP (1) Sequence_ID
FROM MyTable f2
WHERE f2.Dex_ID < f1.Dex_ID
AND f2.Sequence_ID IS NOT NULL
AND f1.Sequence_ID IS NULL
ORDER BY f2.Dex_ID desc
) x
WHERE
f1.Sequence_ID IS NULL

关于SQL Server : Update Query,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49230929/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com