Oracle FAQ | Your Portal to the Oracle Knowledge Grid |
Home -> Community -> Usenet -> c.d.o.misc -> Re: Help with Query
Jeff B schrieb:
> "Charles Hooper" <hooperc2000_at_yahoo.com> wrote in message
> news:4ed69188-f428-46dd-86f0-8850efb9faa9_at_n20g2000hsh.googlegroups.com...
>> On Nov 25, 9:52 am, "Jeff B" <jef..._at_KnoSpam.tds.net> wrote: >>> Hi everyone, >>> >>> I have a book table and in that table it has the book tile, >>> publisher, and >>> type of book it is. example mystery, scifi, etc... >>> >>> I am trying to write a query that brings back a list of every pair of >>> books >>> that have the same publisher and same book type. I have been able to >>> get >>> the following code to work: >>> >>> select publisher_code, type >>> from book >>> group by publisher_code, type >>> having count(*) > 1; >>> >>> which returns the following results: >>> >>> PU TYP >>> -- --- >>> JP MYS >>> LB FIC >>> PE FIC >>> PL FIC >>> ST SFI >>> VB FIC >>> >>> I can not figure out how to get the book title and book code for the >>> books >>> that this result list represents, everything i have tried throws out an >>> error. >>> >>> Can someone help? >>> >>> Thanks, >>> >>> Jeff >> >> I see two possible methods: >> 1. Slide the SQL statement that you have written into an inline view, >> join the inline view to your book table, and then use the >> publisher_code, type columns to drive back into your book table. The >> join syntax may look like one of the following: (publisher_code, type) >> IN (SELECT...) or b.publisher_code=ib.publisher_code and >> b.type=ib.type >> 2. Use analytical functions (COUNT() OVER...) to determine the number >> of matches for the same publisher_code, type columns. Then slide this >> SQL statement into an inline view to retrieve only those records with >> the aliased COUNT() OVER greater than 1. This has the benefit of >> retrieving the matching rows in a single pass. >> >> You will likely find examples of the above approaches in this and the >> comp.databases.oracle.server group. >> >> Charles Hooper >> IT Manager/Oracle DBA >> K&M Machine-Fabricating, Inc.
You want get all pairs of books from your table - to get all permutations usually cross join is used. If you consider (1,2) to be the same as (2,1), then all permutations should be reduced about a half - to achieve it, a simple condition like (j<k) - where j,k are your set items
SQL> with books as (
2 select 'JP' publisher_code,'MYS' type,'The Jupiter Legacy' title from dual union all
3 select 'ST','SFI','The Daleth Effect' from dual union all 4 select 'ST','SFI','A Stainless Steel Rat is Born' from dual union all
5 select 'JP','MYS','Galactic Dreams' from dual union all 6 select 'VB','MYS','The Puppet Masters' from dual union all 7 select 'LB','FIC','The Stainless Steel Rat' from dual union all 8 select 'PE','FIC','One Step from Earth' from dual union all 9 select 'PL','FIC','Planet of No Return' from dual union all 10 select 'VB','FIC','The Technicolor Time Machine' from dual union all 11 select 'LB','FIC','A Rebel in Time' from dual union all 12 select 'PE','FIC','Skyfall' from dual union all 13 select 'PL','FIC','War With the Robots' from dual union all 14 select 'VB','FIC','Stainless Steel Visions' from dual union all 15 select 'ST','FIC','The Menace from Earth' from dual16 )
PU TYP TITLE TITLE -- --- ----------------------------- ----------------------------- JP MYS Galactic Dreams The Jupiter Legacy ST SFI A Stainless Steel Rat is Born The Daleth Effect LB FIC A Rebel in Time The Stainless Steel Rat VB FIC Stainless Steel Visions The Technicolor Time Machine PE FIC One Step from Earth Skyfall PL FIC Planet of No Return War With the Robots
6 rows selected.
If you like to have all your titles in one column, then it'll look like
select a.publisher_code,a.type,a.title
from books a,books b
where a.publisher_code=b.publisher_code
and a.type=b.type
-- and a.title<b.title
Best regards
Maxim Received on Sun Nov 25 2007 - 10:13:51 CST