我想將子查詢的結果保存到表中。如何用子查詢更新多行?
select (b.close - a.close) AS difference from day_ohlcs AS a inner join day_ohlcs AS b ON a.id + 1 = b.id
子查詢的速度快,但是當我試圖把它放在它永遠更新語句,所以我知道我做錯了什麼。
UPDATE day_ohlcs
SET price_change = t.difference
FROM (select (b.close - a.close) AS difference
FROM day_ohlcs AS a
inner join day_ohlcs AS b
ON a.id + 1 = b.id)
AS t
WHERE day_ohlcs.instrument_id = 1;
我不理解什麼?提前致謝。
嗯,你做一個特里普爾加入此表。如果它很大,這可能會影響性能。 – bksi
我只想從一行到另一行的一列的差異。我應該試圖實現一個窗口函數嗎? – Bijan