简体   繁体   English

PostgreSQL计算行之间的差异

[英]PostgreSQL calculate difference between rows

I tried to calculate difference between rows in a field using a query: 我尝试使用查询计算字段中行之间的差异:

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

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

Increase column as output by difference between adjacent rows in fixes. 通过修复中相邻行之间的差异来增加列作为输出。

This is what window functions are for: 这是窗口函数的用途:

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

For more details please see the manual: 有关详细信息,请参阅手册:
http://www.postgresql.org/docs/current/static/tutorial-window.html http://www.postgresql.org/docs/current/static/tutorial-window.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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