在postgresql中移动最大值

moving maximum in postgresql

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

例如,如果我们有一个 table 按 b desc:

排序
a   b
1   5
2   4
1   2
6   2

我想要第三列跟踪 a 到目前为止的最大值:

max_a_sofar
1
2
2
6 

使用max window函数。

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