PostgreSQL:如何通过与之前的运行比较来展示个人最好成绩

PostgreSQL: How to show personal bests by comparing to previous runs

我希望通过查看截至该行月份的“当前”个人最佳成绩在 PostgreSQL 中创建“fastest_run_time”列。例如:

我试过做一些分区和 max/mins 并且已经把它变成了这种格式,但真的看不出从这里去哪里

如果支持语法分区:

select mt.*,
min(run_time) over 
        (partition by run_type 
         order by period 
         rows between unbounded preceding and current row) as fastest_run_time 
from mytbl mt

只是一个子查询:

select run_type, to_char(period, 'YYYY-MM'), run_time, (
    select min(rs.run_time) from run rs
        where rs.period <= run.period
) fastest_run_time from run;

Demo with schema.

结果:

run_type to_char run_time fastest_run_time
A 2021-05
A 2021-06 762 762
A 2021-07 762 762
A 2021-08 720 720
A 2021-09 745 720
A 2021-10 745 720
A 2021-11 745 720
A 2021-12 691 691