簡體   English   中英

選擇行間差異(Postgres)

[英]Select Difference Between Rows (Postgres)

隨着時間的推移,我正在聚集Facebook“喜歡”的網址計數。 每小時,我檢查“喜歡”計數並將其插入帶時間戳的行。 如何獲得新行和前一小時行之間的總差異?

例如,

  1. 在下午2點為id = 1插入total = 5
  2. insert total = 12,diff_since_last = 7 for id = 1 at 3pm

謝謝!

這應該做到這一點。

SELECT id, 
       total, 
       total - lag(total) over (partition by id order by timestamp_column) as diff_since_last
FROM the_table_with_no_name

當我嘗試a_horse_with_no_name的解決方案時,我不得不刪除'id by id'部分以獲得結果(我正在使用PG 9.2)

SELECT id,total,total - lag(total)over(order by timestamp_column)as diff_since_last FROM the_table_with_no_name

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM