Combine rows when the end time of one is the start time of another (Oracle)

前端 未结 5 432
旧时难觅i
旧时难觅i 2021-01-18 15:49

I just can\'t seem to get this query figured out. I need to combine rows of time-consecutive states into a single state.

This question is similar to the question fou

5条回答
  •  迷失自我
    2021-01-18 16:27

    Maybe this? (I don't have a SQL machine to run it on)

    WITH
      sequenced_data AS
    (
      SELECT
        ROW_NUMBER() OVER (PARTITION BY name                ORDER BY start_inst) NameSequenceID,
        ROW_NUMBER() OVER (PARTITION BY name, code, subcode ORDER BY start_inst) NameStateSequenceID,
        *
      FROM
        data
    )
    SELECT
      name,
      MIN(start_inst) start_inst,
      MAX(end_inst)   end_inst,
      code,
      subcode
    FROM
      sequenced_data
    GROUP BY
      name,
      code,
      subcode,
      NameSequenceID - NameStateSequenceID
    

提交回复
热议问题