Your expected output is probably wrong, the 3rd leave start on 7/1/2010 according to your date not on 7/1/2011.
This combines the rows from the first "leave" up to the next "active" row:
SELECT emplid, MIN(effdt) AS LEAVE_START_DATE, x AS LEAVE_END_DATE
FROM
(
SELECT t.*,
MIN(CASE WHEN empl_status = 'active' THEN effdt END)
OVER (PARTITION BY emplid
ORDER BY effdt, sequence
ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING) AS x
FROM dropme AS t
QUALIFY empl_status = 'LEAVE'
) AS dt
GROUP BY 1,xDieter
↧