比较Postgres中的数组

时间:2020-09-18 06:20:20

标签: sql arrays postgresql common-table-expression

enter image description here

以上历史记录表捕获了当前存在标签的时间和区域。 我想找出标签何时进入或退出区域。 尝试过

    more_data as (
         select tag_id,
                update_time,
                zone_ids as current_zones,
                lag(zone_ids, 1) over (partition by tag_id order by update_time asc) as prev_zones
         from tag_hist
         order by update_time asc
     )
select *
from more_data
order by tag_id, update_time asc;

现在我有 enter image description here

现在我想比较当前区域和上一个区域,以识别进入/退出的区域

with entered zones = current_zones - prev_zones
with exited zones = prev_zones - current_zones

最后我想拥有类似的东西 enter image description here

尝试http://sqlfiddle.com/#!17/a7db2/3

2 个答案:

答案 0 :(得分:1)

如果能解决您的需求,您可以尝试以下

with more_data as (
         select tag_id,
                update_time,
                zone_ids as current_zones,
                lag(zone_ids, 1) over (partition by tag_id order by update_time asc) as prev_zones
         from tag_hist
         order by update_time asc
     )
select t.*,
   (select array(select unnest(current_zones::int[]) except select unnest(prev_zones::int[]))) as "entered zones",
   (select array(select unnest(prev_zones::int[]) except select unnest(current_zones::int[]))) as "exited zones"
from more_data t
order by tag_id, update_time asc;

db<>fiddle

答案 1 :(得分:1)

似乎对未嵌套的数组元素进行操作比对整个数组更容易:

with tag_hist_row_numbers as (
-- add row numbers 
    select *, row_number() over (order by tag_id, update_time) as rn
    from tag_hist
),
tag_hist_enter_exit as (
-- unnest zone_ids and find enters/exits
    select 
        tag_id, zone_id, update_time, rn,
        rn - 1 is distinct from lag(rn) over w as enter,
        rn + 1 is distinct from lead(rn) over w as exit
    from tag_hist_row_numbers
    cross join unnest(zone_ids) as zone_id
    window w as (partition by tag_id, zone_id order by update_time)
),
tag_hist_times as (
-- assign enter/exit times
    select 
        tag_id, 
        zone_id, 
        enter,
        update_time as enter_time,
        case when exit then update_time else lead(update_time) over w end as exit_time
    from tag_hist_enter_exit
    where enter or exit
    window w as (order by tag_id, zone_id, update_time)
)
select
-- remove redundant rows
-- rows with exit are useless now
    tag_id, 
    zone_id, 
    enter_time,
    exit_time
from tag_hist_times
where enter
order by tag_id, zone_id

SQLFiddle.

如果您希望最终结果采用这种格式,则可以将每个zone_id的时间汇总到数组中。