WCM Forum

WCM Forum (http://www.wcm.at/forum/index.php)
-   Programmierung (http://www.wcm.at/forum/forumdisplay.php?f=17)
-   -   Oracle 10g Rekursive Tabelle als Baumstruktur in Select (http://www.wcm.at/forum/showthread.php?t=208006)

3of4 17.01.2007 12:29

Oracle 10g Rekursive Tabelle als Baumstruktur in Select
 
Ich soll für unser Schulprojekt eine rekursive Beziehung auf sich selbst in einem Select abbilden (also z.B. wie im Forum das Drop-Downmenü).

Es gibt eine Tabelle Produkt mit ID, Produkt_ID, Bezeichnung, wobei Produkt_ID wiederum auf Produkt referenziert.

Datenmässig schaut es momentan so aus:
Code:

ID                Produkt_ID                Bezeichnung
1                null                Produkt1
2                null                Produkt2
3                null                Produkt3
4                1                Produkt1.1
5                1                Produkt1.2
6                3                Produkt3.1
7                2                Produkt2.1
8                1                Produkt1.3

Die Ausgabe sollte gruppiert nach Produktgruppen, sortiert nach Name erfolgen:
Code:

-Produkt1
 |-Produkt1.1
 |-Produkt1.2
 |-Produkt1.3
-Produkt2
 |-Produkt2.1
-Produkt3
 |-Produkt3.1

Die Tiefe dieser Struktur ist variabel, kann also nicht auf X Ebenen beschränkt werden...

3of4 17.01.2007 12:47

ps: hab noch meinen Versuch dazugestellt, ich arbeite allerdings mit der Originaltabelle, welche nicht ganz dem Schema entspricht, aber die Logik sollte ja gleich sein:
Code:

SQL> select * from stat_produkt;

        ID PRODUKT_ID EINHEIT_ID CODE                BEZEICHNUNG     
---------- ---------- ---------- -------------------- ----------------
        26        24            p21                  produkt21
        23                    17 p1                  Produkt1
        24                      p2                  produkt2
        25        23        19 p11                  produkt11
        27        24        18 p22                  Produkt22

SQL>
SQL> column padded_bez format a20
SQL>
SQL> Select lpad(' ',(level-1)*2) || Bezeichnung as padded_bez
  2  , ID , Produkt_ID, Einheit_ID, level
  3  from stat_produkt connect by prior Id=Produkt_id;

PADDED_BEZ                  ID PRODUKT_ID EINHEIT_ID      LEVEL
-------------------- ---------- ---------- ---------- ----------
produkt11                    25        23        19          1
produkt21                    26        24                    1
Produkt22                    27        24        18          1

Produkt1                    23                    17          1
  produkt11                  25        23        19          2
produkt2                    24                                1
  produkt21                  26        24                    2
  Produkt22                  27        24        18          2

8 Zeilen ausgewählt.

Was mich stört, ist dass ich diese roten Unterprodukte auch oben nocheinmal dabeihabe :/


Alle Zeitangaben in WEZ +2. Es ist jetzt 02:07 Uhr.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
© 2009 FSL Verlag