Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

How to return only 1 row if multiple duplicate rows and still return rows that are not duplicates?

1058268Mar 29 2017 — edited Mar 30 2017

Hi everyone,

I am working on Oracle 11g DB and i am using plsql allround automation tool. I am trying to pull some unique records from a list of duplicates based on the date but i ran into a issue where the timing is also same for the same request id's

Example:

create table temp

(request_id   varchar2(50),

created_date  date,

Status        varchar2(50));

insert into temp(request_id,created_date,status) values ('CF-0000001',to_date('08/26/2009 13:07:01','MM/DD/YYYY HH24:MI:SS'),'For Review');

insert into temp(request_id,created_date,status) values ('CF-0000001',to_date('08/26/2009 13:07:01','MM/DD/YYYY HH24:MI:SS'),'Completed');

insert into temp(request_id,created_date,status) values ('CF-0000112',to_date('08/26/2009 13:07:01','MM/DD/YYYY HH24:MI:SS'),'For Review');

insert into temp(request_id,created_date,status) values ('CF-0000113',to_date('08/26/2009 13:07:01','MM/DD/YYYY HH24:MI:SS'),'For Review');

insert into temp(request_id,created_date,status) values ('CF-0000114',to_date('08/26/2009 13:07:01','MM/DD/YYYY HH24:MI:SS'),'Completed');

insert into temp(request_id,created_date,status) values ('CF-0000115',to_date('08/26/2009 13:07:01','MM/DD/YYYY HH24:MI:SS'),'Completed');

select * from temp;

REQUEST_IDSTATUSCREATED_DATE
1CF-0000001For Review8/26/2009 1:07:01 PM
2CF-0000001Completed8/26/2009 1:07:01 PM
3CF-0000112For Review8/26/2009 1:07:01 PM
4CF-0000113For Review8/26/2009 1:07:01 PM
5CF-0000114Completed8/26/2009 1:07:01 PM
6CF-0000115Completed8/26/2009 1:07:01 PM

Now if i tried to pull the unique request id,status code based on the complete date then i wrote a query like this

select t.request_id, t.Status, t.created_date

                 from temp t

                inner join (select request_id,

                                  max(created_date) as MaxDate

                             from temp

                            group by request_id) tm

                   on t.request_id = tm.request_id

                  and t.created_date = tm.MaxDate

                  order by t.request_id

REQUEST_IDSTATUSCREATED_DATE
1CF-0000001For Review8/26/2009 1:07:01 PM
2CF-0000001Completed8/26/2009 1:07:01 PM
3CF-0000112For Review8/26/2009 1:07:01 PM
4CF-0000113For Review8/26/2009 1:07:01 PM
5CF-0000114Completed8/26/2009 1:07:01 PM
6CF-0000115Completed8/26/2009 1:07:01 PM

AS you can see the request id 'CF-0000001' is showing twice, i know that the created date time is same and that's why it is showing up.

So my question is, is there any way that i can pull only unique record (doesn't matter which status) or can we completely remove those kind of request id's ?? I tried googling this kind of issue and i found a similar one but i didn't the correct answer (sql - How to return only 1 row if multiple duplicate rows and still return rows that are not duplicates? - Stack Overflo… )Query help

This post has been answered by mathguy on Mar 29 2017
Jump to Answer
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Apr 27 2017
Added on Mar 29 2017
4 comments
6,014 views