Aggregate Overlapping Segments to Measure Effective Length

前端 未结 6 785
我寻月下人不归
我寻月下人不归 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:27

    I had a problem with your "road events", because of you don't describe what is 1st meas, I posit it is period between 0 and 1 without 1.

    so, you can count this with one query:

    with newest_MEAS as (
    select ROAD_ID, MEAS.m, max(year) y
    from road_events
    join (select rownum -1 m 
          from dual 
          connect by rownum -1 <= (select max(TOTAL_ROAD_LENGTH) from road_events) ) MEAS
      on MEAS.m between FROM_MEAS and TO_MEAS
    group by ROAD_ID, MEAS.m )
    select re.event_id, nm.ROAD_ID, re.total_road_length, nm.y, count(nm.m) EVENT_LENGTH
    from newest_MEAS nm
    join road_events re 
      on nm.ROAD_ID = re.ROAD_ID
      and nm.m between re.from_meas and re.to_meas -1
      and nm.y = re.year
    group by re.event_id, nm.ROAD_ID, re.total_road_length, nm.y
    order by event_id
    

    SQL Fiddle

提交回复
热议问题