Home » SQL & PL/SQL » SQL & PL/SQL » Challenging Query (Oracle 11g , Windows 8)
icon7.gif  Challenging Query [message #641561] Tue, 18 August 2015 03:10 Go to next message
rakeshramm
Messages: 175
Registered: September 2006
Location: Oracle4u.com
Senior Member

Want to write a query to avoid the logical duplicate records .(Technically these records are not duplicate ie:
we can avoid records using the distinct keyword)

 create table LOGICAL_DUP
( source      VARCHAR2(100),
  destination VARCHAR2(100),
  distance    NUMBER
) ;

insert into LOGICAL_DUP (SOURCE, DESTINATION, DISTANCE) values ('AA', 'BB', 5000);
insert into LOGICAL_DUP (SOURCE, DESTINATION, DISTANCE) values ('CC', 'DD', 1000);
insert into LOGICAL_DUP (SOURCE, DESTINATION, DISTANCE) values ('FF', 'GG', 1500);
insert into LOGICAL_DUP (SOURCE, DESTINATION, DISTANCE) values ('AA', 'CC', 3000);
insert into LOGICAL_DUP (SOURCE, DESTINATION, DISTANCE) values ('GG', 'FF', 1500);
insert into LOGICAL_DUP (SOURCE, DESTINATION, DISTANCE) values ('BB', 'AA', 5000); 

commit;


Output required is
 AA  BB  5000
 AA  CC  3000
 CC  DD  1000
 FF  GG  1500

Re: Challenging Query [message #641565 is a reply to message #641561] Tue, 18 August 2015 04:14 Go to previous messageGo to next message
Michel Cadot
Messages: 68641
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

Quote:
avoid the logical duplicate records


Define duplicate. There is no duplicates in your example.

Quote:
Output required is


Explain the output. What are the rules that lead to it?

What is challenging here is to have a clear specification.

[Updated on: Tue, 18 August 2015 04:14]

Report message to a moderator

Re: Challenging Query [message #641566 is a reply to message #641565] Tue, 18 August 2015 04:34 Go to previous messageGo to next message
loosecontrol
Messages: 3
Registered: December 2011
Location: Bangalore
Junior Member
I could achieve it, But i am sure some more description is required here. Like why dont you want to select BB AA 5000, but only AA BB 5000 and so on.

I see you want to make sure combination of source & destination unique. Thats what you mean by duplicate. right?

select min(a.source),max(a.destination),a.distance from LOGICAL_DUP a
where a.source in (select b.destination from logical_dup b where b.source=a.destination)
group by a.distance
union
select min(a.source),max(a.destination),a.distance from LOGICAL_DUP a
where a.source not in (select b.destination from logical_dup b where b.source=a.destination)
group by a.distance
Re: Challenging Query [message #641567 is a reply to message #641561] Tue, 18 August 2015 04:49 Go to previous messageGo to next message
bugfox
Messages: 18
Registered: October 2010
Junior Member
select source,
       destination,
       distance
from (select source, 
             destination, 
             distance,
             row_number() over(partition by greatest(source, destination), least(source, destination) order by source, destination) rn
      from logical_dup)
where rn = 1
Re: Challenging Query [message #641575 is a reply to message #641567] Tue, 18 August 2015 06:42 Go to previous messageGo to next message
rakeshramm
Messages: 175
Registered: September 2006
Location: Oracle4u.com
Senior Member

Thank you loosecontrol and bugfox . Both queries works fine to get the desire output

Great Thanks !!!

Re: Challenging Query [message #641576 is a reply to message #641575] Tue, 18 August 2015 06:44 Go to previous message
Michel Cadot
Messages: 68641
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

So no challenge, this is a FAQ.

Previous Topic: Hierarchy flattening and repetition
Next Topic: check the column is numeric or data
Goto Forum:
  


Current Time: Fri Apr 19 22:53:04 CDT 2024