2014-07-22 8 views
7

In Oracle, se ho una tabella definita come ...Qual è la sintassi PostgreSQL equivalente a CONNECT BY di Oracle ... INIZIA CON?

CREATE TABLE taxonomy 
    (
    key NUMBER(11) NOT NULL CONSTRAINT taxPkey PRIMARY KEY, 
    value VARCHAR2(255), 
    taxHier NUMBER(11) 
    ); 
ALTER TABLE 
    taxonomy 
ADD CONSTRAINT 
    taxTaxFkey 
FOREIGN KEY 
    (taxHier) 
REFERENCES 
    tax(key); 

Con questi valori ...

key value taxHier 
0 zero null 
1 one  0 
2 two  0 
3 three 0 
4 four 1 
5 five 2 
6 six  2 

Questa sintassi di query ...

SELECT 
    value 
FROM 
    taxonomy 
CONNECT BY 
    PRIOR key = taxHier 
START WITH 
    key = 0; 

produrrà ...

zero 
one 
four 
two 
five 
six 
three 

Come è fatto in PostgreSQL?

+3

Penso che ci si vuole utilizzare [CON RECURSIVE] (http://www.postgresql.org/docs/9.3/static/queries-with.html) –

+1

http://stackoverflow.com/ q/22626394/330315 –

risposta

12

Utilizzare un RECURSIVE CTE in Postgres:

WITH RECURSIVE cte AS (
    SELECT key, value, 1 AS level 
    FROM taxonomy 
    WHERE key = 0 

    UNION ALL 
    SELECT t.key, t.value, c.level + 1 
    FROM cte  c 
    JOIN taxonomy t ON t.taxHier = c.key 
    ) 
SELECT value 
FROM cte 
ORDER BY level; 

dettagli e collegamenti alla documentazione nella mia risposta precedente:

5

Postgres ha un equivalente da parte del collegamento. Dovrai abilitare il modulo. È disattivato per impostazione predefinita.

Si chiama tablefunc. Supporta alcune interessanti funzionalità crosstab e il familiare "connect by" e "Start With". Ho trovato che funziona molto più eloquentemente e logicamente rispetto al CTE ricorsivo. Se non riesci a farlo funzionare dal tuo DBA, dovresti cercare il modo in cui Erwin lo sta facendo.
È abbastanza robusto da eseguire anche la query del tipo "distinta componenti".

Tablefunc può essere attivata eseguendo questo comando:

CREATE EXTENSION tablefunc; 

Ecco l'elenco dei campi di collegamento appena sollevato dalla documentazione ufficiale.

Parameter:   Description 
relname:   Name of the source relation (table) 
keyid_fld:   Name of the key field 
parent_keyid_fld: Name of the parent-key field 
orderby_fld:  Name of the field to order siblings by (optional) 
start_with:  Key value of the row to start at 
max_depth:   Maximum depth to descend to, or zero for unlimited depth 
branch_delim:  String to separate keys with in branch output (optional) 

Si dovrebbe dare un'occhiata alla pagina di documentazione. È ben scritto e ti darà le opzioni a cui sei abituato. (Nella pagina doc scorrere verso il basso, il suo vicino al fondo.)

Postgreql "Connect by" extension Di seguito si riporta la descrizione di ciò che mettere insieme tale struttura dovrebbe essere come. C'è un sacco di potenziale, quindi non lo farò giustizia, ma qui c'è un colpo per darti un'idea.

connectby(text relname, text keyid_fld, text parent_keyid_fld 
      [, text orderby_fld ], text start_with, int max_depth 
      [, text branch_delim ]) 

Una vera e propria interrogazione sarà simile a questa. Connectby_tree è il nome della tabella. La riga che inizia con "AS" è il modo in cui le colonne vengono denominate. Sembra un po 'sottosopra.

SELECT * FROM connectby('connectby_tree', 'keyid', 'parent_keyid', 'pos', 'row2', 0, '~') 
    AS t(keyid text, parent_keyid text, level int, branch text, pos int); 
+0

Come è possibile aggiungere più colonne alla query?Diciamo che ho bisogno anche di firstName, lastName. –

Problemi correlati