EDIT - drachenstern: see this question for more information on this problem and on the source of his query below:
Can someone please advise some tuning options for the following query? When running for one project it is fast but for all the records it takes hours to finish running. NUM_ROWS in PA table: 2,101,528
I want to group data by non-unique keys by a distinct time range
SELECT project_nbr, status, MIN(aud_timestamp) start_dt, end_dt
FROM (
SELECT a.project_nbr
, status
, aud_timestamp
, ( SELECT MAX(p.aud_timestamp)
FROM pa p
WHERE p.project_nbr = a.project_nbr
AND p.status = a.status
AND p.aud_timestamp >= a.aud_timestamp
AND NOT EXISTS (
SELECT NULL
FROM pa q
WHERE q.project_nbr = p.project_nbr
AND q.status <> p.status
AND q.aud_timestamp < p.aud_timestamp
AND q.aud_timestamp > a.aud_timestamp
)
) end_dt
FROM pa a
)
GROUP BY project_nbr, status, end_dt