Ergebnis 1 bis 2 von 2

Thema: Abfrage soll aus "join" neue spalten erzeugen

  1. #1
    2morpheus ist offline Neuer Benutzer
    Registriert seit
    07.12.2009
    Beiträge
    1

    Standard Abfrage soll aus "join" neue spalten erzeugen

    Moin,
    sorry, mal wieder eine Anfängerfrage:
    ich habe zwei Tabellen:
    Tabelle 1
    Code:
    ID | c1 | c2 |
    2    xx    yy
    3    xy    yz
    4    yd    yf
    Tabelle 2
    Code:
    IDo  |  titel | content
    2        t1     ct12
    2        t2     ct22
    2        t3     ct32 
    3        t1     ct13
    3        t3     ct33
    4        t2     ct24
    4        t3     ct34
    und daraus soll entstehen:
    Code:
    ID | c1 | c2 |  t1  |  t2  |  t3
    2    xx    yy   ct12  ct22  ct32
    3    xy    yz   ct13        ct33
    4    yd    yf         ct24  ct34
    Das sind benutzerdefinierte Felder (Tabelle 2) zu Tabelle 1, die in der DB auf diese Weise abgelegt werden. Jedes Feld bezieht sich auf die ID des Datensatzes in der ersten Tabelle.
    Wäre toll wenn ihr mir mit einem Hinweis helfen könntet.

    Viele Grüße
    Christian

  2. #2
    akretschmer ist offline Super-Moderator
    Registriert seit
    29.10.2006
    Ort
    Nähe Dresden
    Beiträge
    5.337

    Standard

    Zitat Zitat von 2morpheus Beitrag anzeigen
    Moin,
    sorry, mal wieder eine Anfängerfrage:
    ich habe zwei Tabellen:
    Tabelle 1
    Code:
    ID | c1 | c2 |
    2    xx    yy
    3    xy    yz
    4    yd    yf
    Tabelle 2
    Code:
    IDo  |  titel | content
    2        t1     ct12
    2        t2     ct22
    2        t3     ct32 
    3        t1     ct13
    3        t3     ct33
    4        t2     ct24
    4        t3     ct34
    und daraus soll entstehen:
    Code:
    ID | c1 | c2 |  t1  |  t2  |  t3
    2    xx    yy   ct12  ct22  ct32
    3    xy    yz   ct13        ct33
    4    yd    yf         ct24  ct34
    Das sind benutzerdefinierte Felder (Tabelle 2) zu Tabelle 1, die in der DB auf diese Weise abgelegt werden. Jedes Feld bezieht sich auf die ID des Datensatzes in der ersten Tabelle.
    Wäre toll wenn ihr mir mit einem Hinweis helfen könntet.

    Viele Grüße
    Christian
    1. eine DB != eine Tabellenkalkulation

    2.

    Code:
    test=# select * from t1;
     id | c1 | c2
    ----+----+----
      2 | xx | yy
      3 | xy | yz
      4 | yd | yf
    (3 Zeilen)
    
    Zeit: 0,149 ms
    test=*# select * from t2;
     ido | titel | content
    -----+-------+---------
       2 | t1    | ct12
       2 | t2    | ct22
       2 | t3    | ct32
       3 | t1    | ct13
       3 | t3    | ct33
       4 | t2    | ct24
       4 | t3    | ct34
    (7 Zeilen)
    
    Zeit: 0,218 ms
    test=*# select t1.*, array_to_string(array_agg(case when t2.titel = 't1' then t2.content else null end),',') as t1, array_to_string(array_agg(case when t2.titel='t2' then t2.content else null end),',') as t2, array_to_string(array_agg(case when t2.titel = 't3' then t2.content else null end),',') as t3 from t1 left join t2 on t1.id=t2.ido group by 1,2,3;
     id | c1 | c2 |  t1  |  t2  |  t3
    ----+----+----+------+------+------
      2 | xx | yy | ct12 | ct22 | ct32
      3 | xy | yz | ct13 |      | ct33
      4 | yd | yf |      | ct24 | ct34
    (3 Zeilen)
    Ich hoffe, Du durchschaust das Prinzip ;-)

    Andreas
    --
    ads Das hatte dir akretschmer gestern schon gesagt und siehe da sobald du das machst, funktioniert es ;-)
    https://www.xing.com/profile/Andreas_Kretschmer7

Berechtigungen

  • Neue Themen erstellen: Nein
  • Themen beantworten: Nein
  • Anhänge hochladen: Nein
  • Beiträge bearbeiten: Nein
  •  

Links: Tobias Bauer - IT- und Funkdienstleistungen   -   Bilderrahmen-kaufen.de - Der Onlineshop für Bilderrahmen & Fotorahmen   -   Funkforum