PostgreSQL calculate difference between rows

前端 未结 1 376
孤独总比滥情好
孤独总比滥情好 2020-12-08 07:16

I tried to calculate difference between rows in a field using a query:

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

     year | month | fixes    | incre         


        
相关标签:
1条回答
  • 2020-12-08 08:02

    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

    0 讨论(0)
提交回复
热议问题