I have data in the table similar to below
Emp Date Code
--- -------- ----
E1 11/1/2012 W
E1 11/1/2012 V
E2 11/1/2012 W
E1 11/2/2012 W
E1 11/3/2012 W
E1 11/4/2012 W
E1 11/5/2012 W
I want to get a list of employees between a date range (say, over the last 3 months) that has been working on the W code conditionally for 5 days with a date range in the output. Each employee can have several records in one day with different codes.
Expected Result
Emp Date-Range
--- ----------
E1 11/1 -11/5
Below I tried, but I did not come close to the exit that I was looking for
SELECT distinct user, MIN(date) startdate, MAX(date) enddate
FROM (SELECT user, date, (TRUNC(date) - ROWNUM) tmpcol
FROM (SELECT user, date
FROM tablename
where date between to_date('10/01/2012','mm/dd/yyyy') and to_date('10/03/2012','mm/dd/yyyy')
ORDER BY user, date) ot) t
GROUP BY user, tmpcol
ORDER BY user, startdate;
If Emp E1 has been running for 10 consecutive days, it should be listed twice in the output with both date ranges. If E1 has been working for 9 consecutive days (from 11/1 to 11/9), it should be indicated only once with a date range from 11/1 to 11/9.
, . - Oracle 10G PL/SQL.