dbTalk Databases Forums  

Re: SELECT & recursion

comp.databases.theory comp.databases.theory


Discuss Re: SELECT & recursion in the comp.databases.theory forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Christopher Browne
 
Posts: n/a

Default Re: SELECT & recursion - 03-19-2006 , 01:29 PM






"Mike Carroll" <mcarroll (AT) pobox (DOT) com> wrote:
Quote:
If I'm using a procedural language, I can implement this by recursion:
starting with the given object, search upwards for parents until there
are no more. I don't see how to do this with a SELECT statement. Is
there some SQL/SELECT construct that I'm overlooking?
It is called "WITH", and was first implemented in the SQL standard
fashion in DB2. Most SQL implementations don't support it yet...

The notion is thus...

WITH recursive_tab (col1, col2, col3) as
(select col1, col2, col3 from some_table root
where [conditions indicating "root" nodes]

union all

select children.col1, children.col2, children.col3
from some_table children, recursive_tab
where [conditions that connect children to elements already in
recursive_tab]
)
select * from recursive_tab;

Notice that in the second subquery, recursive_tab refers to itself;
that's where the recursion takes place.

This is very much like the LET form found in Lisp and ML, and could
presumably be used nonrecursively to short-hand initializations for
queries...

Oracle has had a "CONNECT BY" structure to handle network/graph
problems; it has never been standardized.
--
let name="cbbrowne" and tld="gmail.com" in name ^ "@" ^ tld;;
http://linuxdatabases.info/info/slony.html
"...make -k all to compile everything in the core distribution. This
will take anywhere from 15 minutes (on a Cray Y-MP) to 12 hours."
-- X Window System Release Notes


Reply With Quote
  #2  
Old   
Bart Wakker
 
Posts: n/a

Default Re: SELECT & recursion - 03-20-2006 , 01:38 AM






On 2006-03-19, Christopher Browne <cbbrowne (AT) acm (DOT) org> wrote:
Quote:
Oracle has had a "CONNECT BY" structure to handle network/graph
problems; it has never been standardized.
Which looks like this:

select * from hier_tab start with id=x
connect by prior id=parent

'prior id' means the one coming 'before' the child, which is the parent,
i.e. in this fashion you search from child to parent.

you can also search from parent to all its descendants (children, grand
children etc):

select * from hier_tab start with id=x
connect by prior parent=id

will give you the entire tree below some parent

I didn't know about the proposed recursive definition using WITH, I'll sure
give that a try (have used oracles construct for several years).


Reply With Quote
  #3  
Old   
vc
 
Posts: n/a

Default Re: SELECT & recursion - 03-21-2006 , 02:34 PM




Bart Wakker wrote:
[...]

Quote:
select * from hier_tab start with id=x
connect by prior id=parent

'prior id' means the one coming 'before' the child, which is the parent,
i.e. in this fashion you search from child to parent.

you can also search from parent to all its descendants (children, grand
children etc):

select * from hier_tab start with id=x
connect by prior parent=id

will give you the entire tree below some parent

You are confused -- the way 'connect by' behaves in your examples is
just the opposite to what you've described.



Reply With Quote
  #4  
Old   
Bart Wakker
 
Posts: n/a

Default Re: SELECT & recursion - 03-22-2006 , 06:33 AM



On 2006-03-21, vc <boston103 (AT) hotmail (DOT) com> wrote:

Quote:
You are confused -- the way 'connect by' behaves in your examples is
just the opposite to what you've described.

Indeed.

The DB2 solution using WITH, doesn't work for oracle b.t.w.: the result is
ORA-32031: illegal reference of a query name in WITH clause.


Reply With Quote
  #5  
Old   
vc
 
Posts: n/a

Default Re: SELECT & recursion - 03-22-2006 , 09:18 AM




Bart Wakker wrote:
Quote:
On 2006-03-21, vc <boston103 (AT) hotmail (DOT) com> wrote:

You are confused -- the way 'connect by' behaves in your examples is
just the opposite to what you've described.


Indeed.

The DB2 solution using WITH, doesn't work for oracle b.t.w.: the result is
ORA-32031: illegal reference of a query name in WITH clause.
That's because Oracle does not implement the standard recursive query
(a la DB2).



Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.