Need some help in modifying existing initial query based on following data set?
create table date_test (id number, job_Num varchar2(50), job_detail varchar2(50), release_date date);
insert into date_test (id, job_Num, job_detail, release_date) values (1, 'A11111', 'TEST1', to_date('06/06/2002', 'mm/dd/yyyy'));
insert into date_test (id, job_Num, job_detail, release_date) values (2, 'X88888', 'TEST2', to_date('07/07/2005', 'mm/dd/yyyy'));
insert into date_test (id, job_Num, job_detail, release_date) values (3, 'P99999', 'TEST3', to_date('08/09/1987', 'mm/dd/yyyy'));
insert into date_test (id, job_Num, job_detail, release_date) values (4, 'H88888', 'TEST4', to_date('07/03/1999', 'mm/dd/yyyy'));
insert into date_test (id, job_Num, job_detail, release_date) values (5, 'B44444', 'TEST5', to_date('08/15/2010', 'mm/dd/yyyy'));
insert into date_test (id, job_Num, job_detail, release_date) values (6, 'U22222', 'TEST6', to_date('09/18/2001', 'mm/dd/yyyy'));
insert into date_test (id, job_Num, job_detail, release_date) values (7, 'A11111', 'TEST7', to_date('06/06/2001', 'mm/dd/yyyy'));
insert into date_test (id, job_Num, job_detail, release_date) values (8, 'A11111', 'TEST8', to_date('06/06/2010', 'mm/dd/yyyy'));
insert into date_test (id, job_Num, job_detail, release_date) values (9, 'H88888', 'TEST9', to_date('07/03/2010', 'mm/dd/yyyy'));
insert into date_test (id, job_Num, job_detail, release_date) values (10, 'B44444', 'TEST10', to_date('08/15/1988', 'mm/dd/yyyy'));
insert into date_test (id, job_Num, job_detail, release_date) values (11, 'B44444', 'TEST11', to_date('08/15/1996', 'mm/dd/yyyy'));
insert into date_test (id, job_Num, job_detail, release_date) values (12, 'B44444', 'TEST12', to_date('08/15/2002', 'mm/dd/yyyy'));
insert into date_test (id, job_Num, job_detail, release_date) values (14, 'A11111', 'TEST13', to_date('07/07/1985', 'mm/dd/yyyy'));
insert into date_test (id, job_Num, job_detail, release_date) values (15, 'A11111', 'TEST14', NULL);
insert into date_test (id, job_Num, job_detail, release_date) values (8, 'A11111', 'TEST15', to_date('06/06/2001', 'mm/dd/yyyy'));
select *
from
(select id,
job_num,
job_detail,
release_date,
dense_rank() over (partition by job_num order by
release_date desc) rnk
from date_test
where release_date is not null)
where rnk <= 3
Results:
ID JOB_NUM JOB_DETAIL RELEASE_DATE RNK
---------- ---------- ---------- ------------ ----------
8 A11111 TEST8 06-JUN-10 1
1 A11111 TEST1 06-JUN-02 2
7 A11111 TEST7 06-JUN-01 3
8 A11111 TEST15 06-JUN-01 3
5 B44444 TEST5 15-AUG-10 1
12 B44444 TEST12 15-AUG-02 2
11 B44444 TEST11 15-AUG-96 3
9 H88888 TEST9 03-JUL-10 1
4 H88888 TEST4 03-JUL-99 2
3 P99999 TEST3 09-AUG-87 1
6 U22222 TEST6 18-SEP-01 1
2 X88888 TEST2 07-JUL-05 1
12 rows selected.
Dagon and few other folks have helped me on this. ID, JOB_NUM, JOB_DETAIL make up the primary key. I want to modify above query to handle data with same date in this case 06-Jun-01 in following manner:
If this situation is encountered only pick records with higher JOB_DETAIL i.e. TEST15 and exclude lower value of JOB_DETAIL field i.e. TEST7 from result. So ideal resulting query will produce:
ID JOB_NUM JOB_DETAIL RELEASE_DATE RNK
---------- ---------- ---------- ------------ ----------
8 A11111 TEST8 06-JUN-10 1
1 A11111 TEST1 06-JUN-02 2
8 A11111 TEST15 06-JUN-01 3
5 B44444 TEST5 15-AUG-10 1
12 B44444 TEST12 15-AUG-02 2
11 B44444 TEST11 15-AUG-96 3
9 H88888 TEST9 03-JUL-10 1
4 H88888 TEST4 03-JUL-99 2
3 P99999 TEST3 09-AUG-87 1
6 U22222 TEST6 18-SEP-01 1
2 X88888 TEST2 07-JUL-05 1
Can it be done?
Thx
Al
create table date_test (id number, job_Num varchar2(50), job_detail varchar2(50), release_date date);
insert into date_test (id, job_Num, job_detail, release_date) values (1, 'A11111', 'TEST1', to_date('06/06/2002', 'mm/dd/yyyy'));
insert into date_test (id, job_Num, job_detail, release_date) values (2, 'X88888', 'TEST2', to_date('07/07/2005', 'mm/dd/yyyy'));
insert into date_test (id, job_Num, job_detail, release_date) values (3, 'P99999', 'TEST3', to_date('08/09/1987', 'mm/dd/yyyy'));
insert into date_test (id, job_Num, job_detail, release_date) values (4, 'H88888', 'TEST4', to_date('07/03/1999', 'mm/dd/yyyy'));
insert into date_test (id, job_Num, job_detail, release_date) values (5, 'B44444', 'TEST5', to_date('08/15/2010', 'mm/dd/yyyy'));
insert into date_test (id, job_Num, job_detail, release_date) values (6, 'U22222', 'TEST6', to_date('09/18/2001', 'mm/dd/yyyy'));
insert into date_test (id, job_Num, job_detail, release_date) values (7, 'A11111', 'TEST7', to_date('06/06/2001', 'mm/dd/yyyy'));
insert into date_test (id, job_Num, job_detail, release_date) values (8, 'A11111', 'TEST8', to_date('06/06/2010', 'mm/dd/yyyy'));
insert into date_test (id, job_Num, job_detail, release_date) values (9, 'H88888', 'TEST9', to_date('07/03/2010', 'mm/dd/yyyy'));
insert into date_test (id, job_Num, job_detail, release_date) values (10, 'B44444', 'TEST10', to_date('08/15/1988', 'mm/dd/yyyy'));
insert into date_test (id, job_Num, job_detail, release_date) values (11, 'B44444', 'TEST11', to_date('08/15/1996', 'mm/dd/yyyy'));
insert into date_test (id, job_Num, job_detail, release_date) values (12, 'B44444', 'TEST12', to_date('08/15/2002', 'mm/dd/yyyy'));
insert into date_test (id, job_Num, job_detail, release_date) values (14, 'A11111', 'TEST13', to_date('07/07/1985', 'mm/dd/yyyy'));
insert into date_test (id, job_Num, job_detail, release_date) values (15, 'A11111', 'TEST14', NULL);
insert into date_test (id, job_Num, job_detail, release_date) values (8, 'A11111', 'TEST15', to_date('06/06/2001', 'mm/dd/yyyy'));
select *
from
(select id,
job_num,
job_detail,
release_date,
dense_rank() over (partition by job_num order by
release_date desc) rnk
from date_test
where release_date is not null)
where rnk <= 3
Results:
ID JOB_NUM JOB_DETAIL RELEASE_DATE RNK
---------- ---------- ---------- ------------ ----------
8 A11111 TEST8 06-JUN-10 1
1 A11111 TEST1 06-JUN-02 2
7 A11111 TEST7 06-JUN-01 3
8 A11111 TEST15 06-JUN-01 3
5 B44444 TEST5 15-AUG-10 1
12 B44444 TEST12 15-AUG-02 2
11 B44444 TEST11 15-AUG-96 3
9 H88888 TEST9 03-JUL-10 1
4 H88888 TEST4 03-JUL-99 2
3 P99999 TEST3 09-AUG-87 1
6 U22222 TEST6 18-SEP-01 1
2 X88888 TEST2 07-JUL-05 1
12 rows selected.
Dagon and few other folks have helped me on this. ID, JOB_NUM, JOB_DETAIL make up the primary key. I want to modify above query to handle data with same date in this case 06-Jun-01 in following manner:
If this situation is encountered only pick records with higher JOB_DETAIL i.e. TEST15 and exclude lower value of JOB_DETAIL field i.e. TEST7 from result. So ideal resulting query will produce:
ID JOB_NUM JOB_DETAIL RELEASE_DATE RNK
---------- ---------- ---------- ------------ ----------
8 A11111 TEST8 06-JUN-10 1
1 A11111 TEST1 06-JUN-02 2
8 A11111 TEST15 06-JUN-01 3
5 B44444 TEST5 15-AUG-10 1
12 B44444 TEST12 15-AUG-02 2
11 B44444 TEST11 15-AUG-96 3
9 H88888 TEST9 03-JUL-10 1
4 H88888 TEST4 03-JUL-99 2
3 P99999 TEST3 09-AUG-87 1
6 U22222 TEST6 18-SEP-01 1
2 X88888 TEST2 07-JUL-05 1
Can it be done?
Thx
Al