Re: consultas jerarquicas en postgresql

Lists: pgsql-es-ayuda
From: Mike(dot)(dot)(dot) <allein(dot)mike(at)gmail(dot)com>
To: "Lista Postgres ES" <pgsql-es-ayuda(at)postgresql(dot)org>
Subject: consultas jerarquicas en postgresql
Date: 2007-11-06 13:59:00
Message-ID: a54f3bec0711060559v707a74b7m414d6bec7ed11517@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-es-ayuda

Buen Dia

Alguien sabe como hacer consultas jerarquicas con postgresql(8.2). En oracle
lo hacia asi

SELECT LEVEL, nombre FROM empleados
START WITH nombre='Ángel'
CONNECT BY n_jefe= PRIOR n_empleado;

Ángel
|-- Antonio
|-- Eva
|-- Carmen
|-- Andrés
|--Carmelo

Gracias por su ayuda

--
Miguel Enrique Guerra Connor
Der Gerechte wird aus dem Glauben leben
GNU/LINUX user 433347


From: "Marco Antonio" <marcoantoniofrias(at)gmail(dot)com>
To: allein(dot)mike(at)gmail(dot)com
Cc: pgsql-es-ayuda(at)postgresql(dot)org
Subject: Re: consultas jerarquicas en postgresql
Date: 2007-11-06 17:53:37
Message-ID: 33e030de0711060953g1cb5d639y6df4a4700d19fe4@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-es-ayuda

On Nov 6, 2007 9:59 AM, Mike... <allein(dot)mike(at)gmail(dot)com> wrote:
>
> Alguien sabe como hacer consultas jerarquicas con postgresql(8.2). En oracle
> lo hacia asi
>
> SELECT LEVEL, nombre FROM empleados
> START WITH nombre='Ángel'
> CONNECT BY n_jefe= PRIOR n_empleado;
>
> Ángel
> |-- Antonio
> |-- Eva
> |-- Carmen
> |-- Andrés
> |--Carmelo
>

igual no existe, pero alternativas hay varias (las que conozco):

1. Ltree, http://www.sai.msu.su/~megera/postgres/gist/ltree/ una descipción:

"Ltree is a PostgreSQL contrib module which contains implementation of
data types, indexed access methods and queries for data organized as a
tree-like structures."

2. pgtreelib, http://postgresql.gr/pgtreelib/ una descipción:

"A functions library to manipulate trees and hierarchical structures
under PL/pgSQL.

The main idea is to separate the real data from the hierarchical
information. This can be achieved by using custom, user created tables
to store the real data and by using this library for the hierarchical
information. The library stores the hierarchical information in its
internal structure, the user then, can access this information by
using these functions and it is also able to create views that combine
the real data with the hierarchical information."

La elección de cual utilizar es tuya, aunque a la opción (2) le falta
porque todavía esta en versión Beta-4 mientras la (1) esta estable.

--
Saludos y abrazos...

Marco Antonio Frias Butrón
Slackware Linux User
Linux Registered User #356229 - http://counter.li.org/
http://marcoantoniofrias.uni.cc


From: Alvaro Herrera <alvherre(at)commandprompt(dot)com>
To: Marco Antonio <marcoantoniofrias(at)gmail(dot)com>
Cc: allein(dot)mike(at)gmail(dot)com, pgsql-es-ayuda(at)postgresql(dot)org
Subject: Re: consultas jerarquicas en postgresql
Date: 2007-11-06 23:56:50
Message-ID: 20071106235650.GM8635@alvh.no-ip.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-es-ayuda

Marco Antonio escribió:
> On Nov 6, 2007 9:59 AM, Mike... <allein(dot)mike(at)gmail(dot)com> wrote:
> >
> > Alguien sabe como hacer consultas jerarquicas con postgresql(8.2). En oracle
> > lo hacia asi
> >
> > SELECT LEVEL, nombre FROM empleados
> > START WITH nombre='Ángel'
> > CONNECT BY n_jefe= PRIOR n_empleado;
> >
> > Ángel
> > |-- Antonio
> > |-- Eva
> > |-- Carmen
> > |-- Andrés
> > |--Carmelo
> >
>
> igual no existe, pero alternativas hay varias (las que conozco):
>

3. Hay una funcion connectby() en contrib/tablefunc que hace algo
parecido.

--
Alvaro Herrera Developer, http://www.PostgreSQL.org/
"Un poeta es un mundo encerrado en un hombre" (Victor Hugo)


From: "Marco Antonio" <marcoantoniofrias(at)gmail(dot)com>
To: "Marco Antonio" <marcoantoniofrias(at)gmail(dot)com>, allein(dot)mike(at)gmail(dot)com, pgsql-es-ayuda(at)postgresql(dot)org
Subject: Re: consultas jerarquicas en postgresql
Date: 2007-11-09 21:20:30
Message-ID: 33e030de0711091320y7201f2eej2067b2113af8c280@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-es-ayuda

Revisando las entradas en el historial de la lista me tope con esta solución:

4. modelo + pl/pgsql de Hubert Lubaczewski:

La página de la implementación (bueno el idioma es polaco... pero se
entiende la idea):

http://www.depesz.com/various/various-sqltrees-implementation.php

En el tutorial/artículo la idea:

...................... ___sql___
...................../.....................\
.postgresql..............____oracle_____
..........|...................../...............|....................\
......linux........solaris..........linux.........windows
.........................................../..........\
.....................................glibc1...glibc2

el modelo de la db:

CREATE TABLE kategorie (
id serial,
name TEXT,
PRIMARY KEY (id)
);

CREATE TABLE powiazania (
parent_id integer NOT NULL references kategorie (id),
child_id integer NOT NULL references kategorie (id),
depth integer
);

algunos datos insertados:

SELECT * FROM kategorie;
id | name
----+---------
1 | sql
2 | oracle
3 | solaris
4 | windows
(4 rows)

SELECT * FROM powiazania;
parent_id | child_id | depth
-----------+----------+-------
1 | 1 | 0
2 | 2 | 0
1 | 2 | 1
3 | 3 | 0
2 | 3 | 1
1 | 3 | 2
4 | 4 | 0
2 | 4 | 1
1 | 4 | 2
(9 rows)

el resultado esperado, después de varias funciones en pl/pgsql:

SELECT id, repeat (' ', getitemlevel(id)) || '+- ' || name FROM
kategorie ORDER BY createtreepath(id);
id | ?column?
----+-----------------
1 | +- sql
8 | +- postgresql
9 | +- linux
2 | +- oracle
3 | +- solaris
5 | +- linux
6 | +- glibc1
7 | +- glibc2
4 | +- windows
(9 rows)

--
Saludos y abrazos...

Marco Antonio Frias Butron
Slackware Linux User
Linux Registered User #356229 - http://counter.li.org/
http://marcoantoniofrias.uni.cc