SQL PostgreSQL 计算行之间的差异
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24691462/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PostgreSQL calculate difference between rows
提问by user2177232
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.
通过修复中相邻行之间的差异增加列作为输出。
回答by a_horse_with_no_name
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