Oracle FAQ | Your Portal to the Oracle Knowledge Grid |
![]() |
![]() |
Home -> Community -> Usenet -> c.d.o.server -> Re: nice sql problem - an ugly heavy solution
If i'm not wrong, i found a solution. But i'm quite unsatisfied. This
solution is fairly heavy, and there should be a simpler way to do it - in a
few lines probably. Moreover, i tested this on a 9i database, and i don't
know if the sys_connect_by_path() exists in previous versions.
My solution is to use the sys_connect_by_path() oracle function to get the first element of the hierarchy for each node. The following gives first and last elements of each hierarchy, with the level of the last element:
select
l, b,
substr (s, 1, instr(s,',')-1 ) s
from
(
select
level l,
b,
substr(sys_connect_by_path(a,',')||',',2) s
from z
connect by prior b = a
start with a not in ( select b from z )
)
And the following gives only the highest level rows for each root node :
select
s, max(l) l
from
(
select
l, b,
substr (s, 1, instr(s,',')-1 ) s
from
(
select
level l,
b,
substr(sys_connect_by_path(a,',')||',',2) s
from z
connect by prior b = a
start with a not in ( select b from z )
)
)
group by s
Joining the two on s and l does the work. But the full query is really ugly
( do not even think to debug it directly ).
select
vx.l, vx.b,
substr(vy.s,1,10) s
from
(
select
l, b,
substr (s, 1, instr(s,',')-1 ) s
from
(
select
level l,
b,
substr(sys_connect_by_path(a,',')||',',2) s
from z
connect by prior b = a
start with a not in ( select b from z )
)
) vx
inner join
(
select
s, max(l) l
from
(
select
l, b,
substr (s, 1, instr(s,',')-1 ) s
from
(
select
level l,
b,
substr(sys_connect_by_path(a,',')||',',2) s
from z
connect by prior b = a
start with a not in ( select b from z )
)
)
group by s
) vy
on vx.l=vy.l and vx.s=vy.s
;
"Ronald" <devnull_at_ronr.nl> wrote in message news:67ce88e7.0201290442.2bd6ad09_at_posting.google.com...
> given table contains > select * from z; > A B > ---------- ---------- > 1 2 > 2 3 > 3 4 > 7 8 > 8 10 > 12 13 > > wanted output should contain > A B > ---------- ---------- > 1 4 > 7 10 > 12 13 > > how ? in plain sql - no plsql. > > select min(a) a, max(b) b > from z > connect by prior b = a > start with a = 1 > / > A B > ---------- ---------- > 1 4 > > is not complete. Any suggestions ? > > tnx, > Ronald. > ----------------------- > http://ronr.nl/unix-dbaReceived on Tue Jan 29 2002 - 10:45:55 CST
![]() |
![]() |