RE: Recursive SQL query?



The closest thing that comes to this would be Oracle's "Connect By
Prior" syntax. I don't know if other database vendors support this.

-----Original Message-----
From: Colin Wetherbee [mailto:cww@xxxxxxxxxxxxxxxx]
Sent: Tuesday, April 15, 2008 11:36 AM
To: Scott Webster Wood
Cc: dbi-users@xxxxxxxx
Subject: Re: Recursive SQL query?

Scott Webster Wood wrote:
let'sjust say I left Property1 blank on the link forWindows and also
in it's parent Desktop but had it filled in on theDesktop parent
Computers (as shown above). If I wanted to find the Property1 for
the sub category but found it blank, I'd like to grab thevalue from
the next parent(s) above it that did have a value.

Is there any way to writethat kind of recursive search right in the
SQL itself rather than doingtons of lookups in an 'until' type
structure in my scripting languageuntil I find a value that's not
null?

This sounds rather like a poor database design.

Each row of a table should contain one "fact", which can then be merged
via JOIN to incorporate supplementary information pertaining to that
single fact. You shouldn't really need to go recursing through a data
tree to come up with all the parts of a single fact.

For your example, which I've omitted in preference of brevity, I would
suggest adding a property table with two columns: primary key for
property id and property description. Then, instead of storing property

description text separately in your categories and categoryLinks tables,

have a foreign key reference the id column in the property table.

In addition, I'd get rid of your categoryLinks table altogether. In the

categories table, for each row that needs a parent, include a foreign
key that references that same table's primary key. Then, OUTER JOIN the

table to itself later.

If your tree is going to be very deep, keep in mind that some databases
impose (reasonable) limits on the number of JOINs that can be in a
single SQL statement. In the end, you may want to do some processing in

a stored procedure, too, rather than relying on a single SQL query to
provide your entire hierarchy.

Just my $0.02.

Also, since the DBI list isn't really the right place to ask database
design questions, you'll probably want to find a more SQL-centric forum
for future questions. PostgreSQL maintains a pgsql-sql list for
SQL-specific questions, for example.

PostgreSQL would probably be a very good choice for your implementation,

by the way, since it has extensive text searching abilities.

Colin
.



Relevant Pages

  • Re: Trying to simplify an sql query
    ... I am using the following sql query which I feel could be simplified, ... I think I have tried the following construct for the subquery ... to risk hanging the database again. ... As month is no part of the primary key and exists will invariably ...
    (comp.databases.oracle.misc)
  • RE: Expression Builder basic question on syntax / Or / how to incorpor
    ... it is always best to avoid unnecessary hits on the database. ... Use only the control name without the Me. ... runtime error on the textbox. ... This SQL query works, ...
    (microsoft.public.access.formscoding)
  • Re: Noob question on how to use an insert query in a command button
    ... in mm/dd/yyyy format (regardless of what your Short Date format have been ... Dim dbLocation 'Location of our Access database file ... Set objADO = CreateObject'Create an ADO connection ... objADO.Execute 'Execute this SQL query ...
    (microsoft.public.access.forms)
  • Re: cgi ordering a table with sql
    ... table, through the SQL query. ... often only values may be replaced by placeholders, ... the prepare should give the database ... implemented within the Perl layer instead of passing the prepared ...
    (perl.beginners)
  • Re: Binding a grid to master/detail related datatables
    ... build my command objects, but it won't update the database." ... recommend Stored Procedures for accessing your database, ... The above SQL Query should give you the data you need. ... Say I have three datatables that are linked ...
    (microsoft.public.dotnet.general)