Re: Recursive use

From: Bruno Wolff III <bruno(at)wolff(dot)to>
To: Alexander Burbello <burbello3000(at)yahoo(dot)com(dot)br>
Cc: Lista Postgres <pgsql-admin(at)postgresql(dot)org>
Subject: Re: Recursive use
Date: 2006-10-04 15:54:59
Message-ID: 20061004155459.GC23343@wolff.to
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-admin

On Wed, Oct 04, 2006 at 10:08:10 -0300,
Alexander Burbello <burbello3000(at)yahoo(dot)com(dot)br> wrote:
>
> I need to know if Postgres do recursive search and how can I do!

There is a contrib module (tablefunc I think) that allows recursive queries.
However in your example below, you don't actuall need recursion, just
self joins of the table.

> I will explain my problem.
>
>
> table COOPERATIVE
> code_cooperative int
> code_coo_father int
>
> I can have 3 level by business rules
>
> 1 - Father
> ----- 2 - Children
> --------- 3 - Grandchildren
>
>
> I would like to have a query asking who is father and granfather
> select grandfather, father from COOPERATIVE where COD_COOPERATIVE = 3
>
> Do the Postgres can solve this problem?
> Could anybody help me?

You can do something like the following (untested, beware of typos):
SELECT a.code_cooperative as child, a.code_coo_father as father,
b.code_coo_father as grandfather
FROM cooperative a, cooperative b
WHERE
a.code_coo_father = b.code_cooperative
AND
a.code_cooperative = 3
;

In response to

Browse pgsql-admin by date

  From Date Subject
Next Message Brad Nicholson 2006-10-04 16:05:02 Re: postgres in HA constellation
Previous Message Bruno Wolff III 2006-10-04 15:43:18 Re: postgres in HA constellation