簡體   English   中英

PostgreSQL計算行之間的差異

[英]PostgreSQL calculate difference between rows

我嘗試使用查詢計算字段中行之間的差異:

Illustrations:
input:year,month,fixes
output:increase

     year | month | fixes    | increase
    ------+-------+----------+-----------
     2006 | 04    |       1  | 0
     2006 | 05    |       4  | 3
     2006 | 06    |       3  | -1

通過修復中相鄰行之間的差異來增加列作為輸出。

這是窗口函數的用途:

select year, 
       month,
       fixes,
       fixes - lag(fixes) over (order by year, month) as increase,
from the_table;

有關詳細信息,請參閱手冊:
http://www.postgresql.org/docs/current/static/tutorial-window.html

暫無
暫無

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

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