我try 使用查询计算字段中行之间的差异:

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;

For more details please see the manual:
http://www.postgresql.org/docs/current/static/tutorial-window.html

Postgresql相关问答推荐

即使密码更改,也可以访问Docker Postgresql数据库

使用regexp获取表名

如何在查询空字段时设置pgtype.comb为默认值?

为什么在PostgreSQL中CPU_INDEX_TUPLE_COST与CPU_TUPLE_COST不同

为什么32632投影中的几何图形并不完美?

数组格式类型的PostgreSQL日期

为什么这里的默认格式不同? (ISO 8601 的两种不同实现)

GORM 不会创建 many2many 关联

将 postgres 从属提升为主 node

联合所有 postgresql Select 子句保留顺序

SQLAlchemy 中使用什么类型存储字节字符串?

PostgreSQL:将时间戳转换为时间 - 或仅从时间戳列中检索时间

Postgres jsonbNOT contains运算符

如何在 postgres 模式中列出关系

在 Postgresql 中拆分逗号分隔的字段并对所有结果表执行 UNION ALL

使用枚举与布尔值?

Rails PG::UndefinedTable:错误:missing FROM-clause entry for table

PostgreSQL:如果不存在则创建表 AS

Rails + Postgres fe_sendauth:no password supplied

判断 PostgreSQL 中的角色是否设置了密码