gpt4 book ai didi

sql - PostgreSQL 计算行之间的差异

转载 作者:行者123 更新时间:2023-11-29 11:07:59 26 4
gpt4 key购买 nike

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

Illustrations:input:year,month,fixesoutput: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;

更多详情请查看手册:
http://www.postgresql.org/docs/current/static/tutorial-window.html

关于sql - PostgreSQL 计算行之间的差异,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24691462/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com