在PostgreSQL中选择不同的窗口函数

时间:2019-05-29 13:05:18

标签: sql postgresql window-functions rolling-computation

我有一个如下数据表:

如何在10步滚动窗口中为每个纵向距离选择不同的连续性?

理想情况下,我希望在“理想”输出列中输入一些结果,其中结果是数组。

谢谢。 enter image description here

1 个答案:

答案 0 :(得分:1)

一种方法使用数组:

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;