Aggregate Overlapping Segments to Measure Effective Length

前端 未结 6 784
我寻月下人不归
我寻月下人不归 2021-02-07 02:08

I have a road_events table:

create table road_events (
    event_id number(4,0),
    road_id number(4,0),
    year number(4,0),
    from_meas number         


        
6条回答
  •  既然无缘
    2021-02-07 02:26

    There is another way to calculate this, with from and to values:

    with 
      part_begin_point as (
        Select distinct road_id, from_meas point
        from road_events be
        union 
        Select distinct road_id, to_meas point
        from road_events ee
      )
    , newest_part as (
      select e.event_id
      , e.road_id
      , e.year
      , e.total_road_length
      , p.point
      , LAG(e.event_id) over (partition by p.road_id order by p.point) prev_event
      , e.to_meas event_to_meas
      from part_begin_point p
      join road_events e
       on p.road_id = e.road_id
       and p.point >= e.from_meas and  p.point < e.to_meas
       and not exists(
            select 1 from road_events ne 
            where e.road_id = ne.road_id
            and p.point >= ne.from_meas and p.point < ne.to_meas
            and (e.year < ne.year or e.year = ne.year and e.event_id < ne.event_id))
      )
    select event_id, road_id, year
    , point from_meas
    , LEAD(point, 1, event_to_meas) over (partition by road_id order by point) to_meas
    , total_road_length
    , LEAD(point, 1, event_to_meas) over (partition by road_id order by point) - point EVENT_LENGTH
    from newest_part
    where 1=1
    and event_id <> prev_event or prev_event is null
    order by event_id, point
    

    SQL Fiddle

提交回复
热议问题