在postgresql中移动最大值

时间:2017-05-18 13:58:32

标签: sql postgresql window-functions

如何在postgresql中实现移动最大值?

例如,如果我们有一个按b desc命令的表:

a   b
1   5
2   4
1   2
6   2

我希望第三列跟踪到目前为止的最大值:

max_a_sofar
1
2
2
6 

1 个答案:

答案 0 :(得分:1)

使用max窗口功能。

select a,b,max(a) over(order by b desc,a) as running_a_max
from t