像這樣的事情會做到這一點,我認爲
SELECT x.Date, x.Close_Price, (((x.Close_Price/y.Close_Price) - 1) * 100) AS '% Change'
FROM
(
SELECT a.Date AS aDate, MAX(b.Date) AS aPrevDate
FROM SomeTable a
INNER JOIN SomeTable b
WHERE a.Date > b.Date
GROUP BY a.Date
) Sub1
INNER JOIN SomeTable x ON Sub1.aDate = x.Date
INNER JOIN SomeTable y ON Sub1.aPrevDate = y.Date
ORDER BY x.Close_Price DESC
的時候,天都沒有一個接一個的(例如,對於非加工時間想記錄)這將應付。
子查詢獲取每個日期,並且最大日期小於它。然後將它與表格連接起來,以獲取該日期和前一個日期的完整記錄,然後可以完成計算。
編輯 - 和更新在此基礎上選擇: -
UPDATE treasury x
INNER JOIN
(
SELECT a.Date AS aDate, MAX(b.Date) AS aPrevDate
FROM treasury a
INNER JOIN treasury b
WHERE a.Date > b.Date
GROUP BY a.Date
) Sub1 ON Sub1.aDate = x.Date
INNER JOIN treasury y ON Sub1.aPrevDate = y.Date
SET x.PercentChange = (((x.Close_Price/y.Close_Price) - 1) * 100)
老實說,我還沒有執行任何事情。我能夠做的唯一的事情就是獲取值,使用PHP進行計算並插入結果。但是,這可能會導致每行2個查詢,並且我有很多行... –