Oracle FAQ Your Portal to the Oracle Knowledge Grid
HOME | ASK QUESTION | ADD INFO | SEARCH | E-MAIL US
 

Home -> Community -> Usenet -> c.d.o.misc -> Re: CONNECT BY / START WITH query to count "families"

Re: CONNECT BY / START WITH query to count "families"

From: Vadim Tropashko <vadimtro_invalid_at_yahoo.com>
Date: 4 Oct 2005 09:31:11 -0700
Message-ID: <1128443471.907448.187410@g14g2000cwa.googlegroups.com>


Vadim Tropashko wrote:

> trippknightly_at_hotmail.com wrote:

> > How could I use CONNECT BY & START WITH in a query to count
> > relationships or families.

With the hint by Leonid Libkin:

<quote>Your equivalence relation is simply reachability (transitive closure) - you make the graph undirected to make this an equivalence relation. Once you have an equivalence relation you can define the set

{s | every element s' equivalent to s is greater than s in the ordering}

This is is clearly first order (rel. calc) and gives you unique representatives of equivalence classes.</quote>

The solution is reduced to the following:

create table input as (
select 1 x, 2 y from dual
union
select 2, 3 from dual
union
select 2, 4 from dual
union
select 6, 7 from dual
union
select 8, 9 from dual);

with STC as ( -- Symmetric transitive closure

   select connect_by_root(x) x, y from (     select x,y from input
    union
    select y,x from input
   ) connect by nocycle x = prior y
) select distinct x from STC s
where not exists (

   select * from STC ss where ss.y<s.x and s.x=ss.x );

X
---
 1
 6
 8 Received on Tue Oct 04 2005 - 11:31:11 CDT

Original text of this message

HOME | ASK QUESTION | ADD INFO | SEARCH | E-MAIL US