I have hierarchical data that bind instances of an object using DATE_FROMand DATE_TO.
See sqlfiddle .
Using CONNECT_BY, I can determine the number of continuous instances for each object, i.e. the length of the "islands", which is basically what I want. For example, this gives the expected island lengths for each object since DATE_FROM2014:
SELECT
T.ENTITY_ID,
MAX(LEVEL) MAX_LEVEL
FROM TEST T
WHERE EXTRACT(YEAR FROM T.DATE_FROM) = 2014
CONNECT BY
T.ENTITY_ID = PRIOR T.ENTITY_ID
AND T.DATE_FROM = PRIOR T.DATE_TO
GROUP BY T.ENTITY_ID
However, what I would like to do is count the lines on the islands, where DATE_FROMthey DATE_TOtake some minimal number of days. I do not want to destroy the hierarchy of the island when I do this.
So, I tried this, but it is wrong. The results are not always what I am looking for.
SELECT
T.ENTITY_ID,
MAX(LEVEL) MAX_LEVEL,
SUM(
CASE WHEN PRIOR T.DATE_TO - PRIOR T.DATE_FROM > 183
THEN 1
ELSE 0
END
) LONG_TERM_COUNT
FROM TEST T
WHERE EXTRACT(YEAR FROM T.DATE_FROM) = 2014
CONNECT BY
T.ENTITY_ID = PRIOR T.ENTITY_ID
AND T.DATE_FROM = PRIOR T.DATE_TO
GROUP BY T.ENTITY_ID
What gives
+-----------+-----------+-----------------+
| ENTITY_ID | MAX_LEVEL | LONG_TERM_COUNT |
+-----------+-----------+-----------------+
| 1 | 4 | 3 |
| 2 | 5 | 4 |
+-----------+-----------+-----------------+
but i'm looking
+-----------+-----------+-----------------+
| ENTITY_ID | MAX_LEVEL | LONG_TERM_COUNT |
+-----------+-----------+-----------------+
| 1 | 4 | 4 |
| 2 | 5 | 4 |
+-----------+-----------+-----------------+
Oracle. .