Order by multiple columns and ROWNUM
Seenu001Jun 19 2011 — edited Jun 20 2011Hi -
I am trying to use rownum in colloboration with order by multiple columns but I guess I am doing something wrong
and I am always getting a single row per emp_id rather than getting single row for a particular emp_id,role_id,inst_id,exp_date.
Below is the table ddl and data insertion script.
====
create table tab_details
(emp_id varchar2(50),
role_id integer,
inst_id integer,
exp_date date,
chk_ind char(1),
trans_type char(1),
trans_date date);
insert all
into tab_details values ('JBRAMS',12,4556,'07/15/2011','Y','U','05/21/2011')
into tab_details values ('JBRAMS',12,4556,'07/15/2011','N','U','05/22/2011')
into tab_details values ('JBRAMS',12,4556,'07/15/2011','N','D','05/23/2011')
into tab_details values ('JBRAMS',12,4556,'07/15/2011','N','U','05/24/2011')
into tab_details values ('KCASOT',98,9001,'08/03/2011','Y','U','04/11/2011')
into tab_details values ('KCASOT',98,9001,'08/03/2011','N','U','04/12/2011')
into tab_details values ('KCASOT',98,9001,'08/03/2011','N','U','04/13/2011')
into tab_details values ('JBRAMS',43,7875,'06/28/2011','Y','U','03/19/2011')
into tab_details values ('JBRAMS',43,7875,'06/28/2011','N','U','03/20/2011')
into tab_details values ('BHAYEN',28,4098,'07/01/2011','Y','U','02/13/2011')
into tab_details values ('BHAYEN',28,4098,'07/01/2011','N','U','02/14/2011')
into tab_details values ('BHAYEN',28,4098,'07/01/2011','N','D','02/15/2011')
into tab_details values ('BHAYEN',28,4098,'07/01/2011','Y','U','02/16/2011')
into tab_details values ('BHAYEN',28,4098,'07/01/2011','N','U','02/17/2011')
into tab_details values ('BHAYEN',28,4098,'07/01/2011','N','U','02/18/2011')
select * from dual;
commit;
====
So for the first four rows inserted above, I need only following row:
'JBRAMS',12,4556,'07/15/2011','N','U','05/22/2011'
Basically, I need only the rows with chk_ind='N' and trans_type<>'D' and if there is a row with
chk_ind='N' and trans_type='U' after trans_type='D" for a emp_id,role_id,inst_id,exp_date then it should be ignored.
Following are the only rows I am looking for:
'JBRAMS',12,4556,'07/15/2011','N','U','05/22/2011'
'KCASOT',98,9001,'08/03/2011','N','U','04/12/2011'
'JBRAMS',43,7875,'06/28/2011','N','U','03/20/2011'
'BHAYEN',28,4098,'07/01/2011','N','U','02/14/2011'
'BHAYEN',28,4098,'07/01/2011','N','U','02/17/2011'
Please share your thought over this.
Thanks,
-Seenu