Mari kita asumsikan saya punya meja seperti ini: Nilai Tanggal ID 1 2021-04-05 100. 1 2021-04-04 50. 1 2021-04-03 25. 1 2021-04-02 5. 2 2021-04-05 80. 2 2021-04-04 20. 2 2021-04-03 15. 2 2021-04-02 10. Aku ingin ...

0
Alec Donald Mather 6 April 2021, 00:04

1 menjawab

Jawaban Terbaik

Mungkin sesuatu seperti ini?

SELECT 
id, 
date,
value, 
(value - prev_value) / prev_value AS pct_change
FROM
(
SELECT 
id, 
date, 
value,
LAG(value) OVER (PARTITION BY id ORDER BY date 
                              ROWS BETWEEN 1 PRECEDING AND 
                                           CURRENT ROW) AS prev_value
FROM
your_table
)
ORDER BY date, id
1
Macter 5 April 2021, 21:44