如何访问同一结果集中后续行的数据

How to accesses data from a subsequent row in the same result set

我需要将参考交易数量列更新为交易数量,但就像参考数量中使用 2050 数量一样,之后需要将交易数量列更新为 1500,您可以在交易数量列中看到,之后一一更新更新。

你能告诉我怎么做吗?

期望的输出应该是(在带有 ReferenceTradeQty = 2050 的行之后):

ReferenceTradeQty
-----------------
1500.00000000
1500.00000000
2100.00000000
6550.00000000
6300.00000000

您可以使用 LEAD 函数从下一行获取信息。 指示性答案是 here.

SELECT t.*, LEAD(TradeQty,1) OVER (ORDER BY AIFTradeDetailsID)
FROM t

您可以在 UPDATE 语句中使用 LEAD() window 函数,如

WITH t2 AS
(
 SELECT *,LEAD(AllotedTradeQty) OVER (ORDER BY Row_num) AS ReferenceTradeQty2
   FROM t
)
UPDATE t
   SET ReferenceTradeQty = t2.ReferenceTradeQty2
  FROM t2
 WHERE t.Row_num = t2.Row_num

Demo