I have a bit of a poser..
create table temp_mich
(
mydate date,
mytime varchar2(4),
job_type varchar2(4),
empid number
)
insert into temp_mich values('24-mar-09','0830','CAP1',1234);
insert into temp_mich values('24-mar-09','0837','CAP1',1234);
insert into temp_mich values('24-mar-09','0848','CAP1',1234);
insert into temp_mich values('24-mar-09','0852','CAP1',1234);
insert into temp_mich values('24-mar-09','0859','CAP2',1234);
insert into temp_mich values('24-mar-09','0900','CAP2',1234);
insert into temp_mich values('24-mar-09','0910','CAP1',1234);
insert into temp_mich values('24-mar-09','0914','CAP1',1234);
insert into temp_mich values('24-mar-09','0930','CAP1',1234);
insert into temp_mich values('24-mar-09','0830','CAP1',5678);
insert into temp_mich values('24-mar-09','0837','CAP1',5678);
insert into temp_mich values('24-mar-09','0848','CAP1',5678);
insert into temp_mich values('24-mar-09','0852','CAP1',5678);
insert into temp_mich values('24-mar-09','0859','CAP2',5678);
insert into temp_mich values('24-mar-09','0900','CAP2',5678);
insert into temp_mich values('24-mar-09','0910','CAP1',5678);
insert into temp_mich values('24-mar-09','0914','CAP1',5678);
insert into temp_mich values('24-mar-09','0930','CAP1',5678);
As you can see, we have a poor design arounf the date and time column with a four digit time being instered rather than just using the date field.
Anyway, from my sql, what I'd like to see is
mydate mytime job_type empid seq
24-MAR-09 0830 CAP1 1234 1
24-MAR-09 0837 CAP1 1234 1
24-MAR-09 0848 CAP1 1234 1
24-MAR-09 0852 CAP1 1234 1
24-MAR-09 0859 CAP2 1234 2
24-MAR-09 0900 CAP2 1234 2
24-MAR-09 0910 CAP1 1234 3
24-MAR-09 0914 CAP1 1234 3
24-MAR-09 0930 CAP1 1234 3
24-MAR-09 0830 CAP1 5678 1
24-MAR-09 0837 CAP1 5678 1
24-MAR-09 0848 CAP1 5678 1
24-MAR-09 0852 CAP1 5678 1
24-MAR-09 0859 CAP2 5678 2
24-MAR-09 0900 CAP2 5678 2
24-MAR-09 0910 CAP1 5678 3
24-MAR-09 0914 CAP1 5678 3
24-MAR-09 0930 CAP1 5678 3
The change to the seq should be on the change of job_type, and it should reset to one on change of empid.
Now, using this
select mydate, mytime, job_type, empid,
dense_rank() over (partition by empid order by empid, job_type) seq from temp_mich ORDER by empid, to_date(mydate|| mytime,'dd-mon-yyhh24mi')
I get
mydate mytime job_type empid seq
24-MAR-09 0830 CAP1 1234 1
24-MAR-09 0837 CAP1 1234 1
24-MAR-09 0848 CAP1 1234 1
24-MAR-09 0852 CAP1 1234 1
24-MAR-09 0859 CAP2 1234 2
24-MAR-09 0900 CAP2 1234 2
24-MAR-09 0910 CAP1 1234 1
24-MAR-09 0914 CAP1 1234 1
24-MAR-09 0930 CAP1 1234 1
24-MAR-09 0830 CAP1 5678 1
24-MAR-09 0837 CAP1 5678 1
24-MAR-09 0848 CAP1 5678 1
24-MAR-09 0852 CAP1 5678 1
24-MAR-09 0859 CAP2 5678 2
24-MAR-09 0900 CAP2 5678 2
24-MAR-09 0910 CAP1 5678 1
24-MAR-09 0914 CAP1 5678 1
which is close, but not close enough.
Ayy pointers would be very much appreciated.
ahh, the old "group em, carry down and then rank" problem....
<b>we start by marking the interesting records:</b>
ops$tkyte%ORA10GR2> select mydate, mytime, job_type, empid,
2 decode(lag(job_type) over (partition by empid order by mydate, mytime), job_type, to_number(null), row_number() over (partition by empid order by mydate, mytime)) rn
3 from t
4 /
MYDATE MYTI JOB_ EMPID RN
--------- ---- ---- ---------- ----------
24-MAR-09 0830 CAP1 1234 1
24-MAR-09 0837 CAP1 1234
24-MAR-09 0848 CAP1 1234
24-MAR-09 0852 CAP1 1234
24-MAR-09 0859 CAP2 1234 5
24-MAR-09 0900 CAP2 1234
24-MAR-09 0910 CAP1 1234 7
24-MAR-09 0914 CAP1 1234
24-MAR-09 0930 CAP1 1234
24-MAR-09 0830 CAP1 5678 1
24-MAR-09 0837 CAP1 5678
24-MAR-09 0848 CAP1 5678
24-MAR-09 0852 CAP1 5678
24-MAR-09 0859 CAP2 5678 5
24-MAR-09 0900 CAP2 5678
24-MAR-09 0910 CAP1 5678 7
24-MAR-09 0914 CAP1 5678
24-MAR-09 0930 CAP1 5678
18 rows selected.
<b>Now, we have to carry down the last non-null value in the group - we could use ignore nulls in 10g, but you are 9i...</b>
ops$tkyte%ORA10GR2> select mydate, mytime, job_type, empid,
2 max(rn) over (partition by empid order by mydate, mytime) grp
3 from (
4 select mydate, mytime, job_type, empid,
5 decode(lag(job_type) over (partition by empid order by mydate, mytime), job_type, to_number(null), row_number() over (partition by empid order by mydate, mytime)) rn
6 from t
7 )
8 /
MYDATE MYTI JOB_ EMPID GRP
--------- ---- ---- ---------- ----------
24-MAR-09 0830 CAP1 1234 1
24-MAR-09 0837 CAP1 1234 1
24-MAR-09 0848 CAP1 1234 1
24-MAR-09 0852 CAP1 1234 1
24-MAR-09 0859 CAP2 1234 5
24-MAR-09 0900 CAP2 1234 5
24-MAR-09 0910 CAP1 1234 7
24-MAR-09 0914 CAP1 1234 7
24-MAR-09 0930 CAP1 1234 7
24-MAR-09 0830 CAP1 5678 1
24-MAR-09 0837 CAP1 5678 1
24-MAR-09 0848 CAP1 5678 1
24-MAR-09 0852 CAP1 5678 1
24-MAR-09 0859 CAP2 5678 5
24-MAR-09 0900 CAP2 5678 5
24-MAR-09 0910 CAP1 5678 7
24-MAR-09 0914 CAP1 5678 7
24-MAR-09 0930 CAP1 5678 7
18 rows selected.
<b>and then we rank the groups...</b>
ops$tkyte%ORA10GR2> select mydate, mytime, job_type, empid,
2 dense_rank() over (partition by empid order by grp) dr
3 from (
4 select mydate, mytime, job_type, empid,
5 max(rn) over (partition by empid order by mydate, mytime) grp
6 from (
7 select mydate, mytime, job_type, empid,
8 decode(lag(job_type) over (partition by empid order by mydate, mytime), job_type, to_number(null), row_number() over (partition by empid order by mydate, mytime)) rn
9 from t
10 )
11 )
12 order by empid, mydate, mytime
13 /
MYDATE MYTI JOB_ EMPID DR
--------- ---- ---- ---------- ----------
24-MAR-09 0830 CAP1 1234 1
24-MAR-09 0837 CAP1 1234 1
24-MAR-09 0848 CAP1 1234 1
24-MAR-09 0852 CAP1 1234 1
24-MAR-09 0859 CAP2 1234 2
24-MAR-09 0900 CAP2 1234 2
24-MAR-09 0910 CAP1 1234 3
24-MAR-09 0914 CAP1 1234 3
24-MAR-09 0930 CAP1 1234 3
24-MAR-09 0830 CAP1 5678 1
24-MAR-09 0837 CAP1 5678 1
24-MAR-09 0848 CAP1 5678 1
24-MAR-09 0852 CAP1 5678 1
24-MAR-09 0859 CAP2 5678 2
24-MAR-09 0900 CAP2 5678 2
24-MAR-09 0910 CAP1 5678 3
24-MAR-09 0914 CAP1 5678 3
24-MAR-09 0930 CAP1 5678 3
18 rows selected.