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
Here is a solution using a recursive query instead of analytic functions (as suggested by @wildplasser):
SELECT name, code, subcode, MIN(start_inst) AS start_inst, MAX(end_inst) AS end_inst
FROM (SELECT name,
start_inst,
end_inst,
code,
subcode,
MIN(CONNECT_BY_ROOT (start_inst)) AS root_start
FROM data d
CONNECT BY PRIOR name = name
AND PRIOR end_inst = start_inst
AND PRIOR code = code
AND PRIOR subcode = subcode
GROUP BY name, start_inst, end_inst, code, subcode)
GROUP BY name, code, subcode, root_start;
The connect by
clause in the innermost query causes the data to be returned in a hierarchical fashion. connect_by_root
gives us the value at the root of each branch. Because we don't have a good candidate for a start with
clause, we'll get all child rows (where end_inst
equals another row's start_inst
and all other columns are the same) multiple times: once as a root and once (or more) as a branch. Taking the min
of the root eliminates these extra rows while giving us a value to group on in the outer query.
In the outer query, we perform another group by
to consolidate the rows. The difference is that, in this case, we have root_start
there as well to identify which rows are consecutive and therefore need to be consolidated.