select PostgreSQL 中不同的 window 函数

select distinct window function in PostgreSQL

我有一个数据 table 如下所示:

你如何select 10 步滚动中每个纵向距离的不同连续性 window?

理想情况下,我希望理想输出列中的结果是数组。

谢谢。

一种方法使用数组:

select t.*,
       (select count(distinct c) from unnest(ar) c) as num_distinct
from (select t.*,
             array_agg(continuity) over (order by distance rows between 9 preceding and current row) ar
      from t
     ) t;

编辑:

或者,如果您需要这些值,请聚合它们:

select t.*,
       (select array_agg(distinct c) from unnest(ar) c) as num_distinct
from (select t.*,
             array_agg(continuity) over (order by distance rows between 9 preceding and current row) ar
      from t
     ) t;