【问题标题】:PostgreSQL calculate difference between rowsPostgreSQL计算行之间的差异
【发布时间】:2014-10-24 06:57:25
【问题描述】:

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

插图: 输入:年、月、修正 输出:增加 年份 |月 |修复 |增加 ------+-------+----------+---------- 2006 | 04 | 1 | 0 2006 | 05 | 4 | 3 2006 | 06 | 3 | -1

通过修复中相邻行之间的差异增加列作为输出。

【问题讨论】:

  • 你期望什么输出?
  • 增加列作为输出

标签: sql postgresql


【解决方案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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多