简体   繁体   中英

select distinct window function in PostgreSQL

I have a data table like the following:

How do you select the distinct continuities for each Longitudinal distance in a 10-step rolling window?

Ideally, I'd want something in the Ideal output column in which the results are arrays.

Thanks. 在此处输入图片说明

One method uses arrays:

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;

EDIT:

Or, if you want the values, aggregate them:

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM