简体   繁体   中英

Subtract the value of the most recent row with the value of the previous row (day -1)

I have a table with incremental value for each day. I'd like to subtract the value of the most recent row with the value of the previous row (day -1)

For example, this would be perfect :

SUM(value) OVER (PARTITION BY item_name ORDER BY date ROWS BETWEEN 1 PRECEDING AND CURRENT ROW)

However, I would need to apply a DIFF function instead of a SUM function.

只需使用lag()

select val - lag(val) over (partition by item_name order by date)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM