Re: Tabelle umgruppieren ?

Andreas Kretschmer <[email protected]> Sun, 27 Oct 2019 17:24:48 +0100
Newsgroups gmane.comp.db.postgresql.german
Message-ID <[email protected]>

Am 27.10.19 um 13:09 schrieb Nicola Tiling:
> ich hätte gerne dieses Ergebnis das quasi einen*Kabelweg von switch_A und port zu switch_B und port abbildet*.
> Eine kabelnummer soll/kann logischerweise  genau 2x in der Abfrage vorkommen weil ein Kabel auf je einen Port von zwei Switchen geht

das ginge z.B. so:

test=*# select * from kabel ;
   switch  | port | kabel_nr
----------+------+----------
  cisco1   |    4 |       12
  cisco1   |    5 |       13
  cisco1   |    6 |       14
  cisco2   |    6 |       14
  cisco2   |   48 |       10
  juniper1 |    8 |       12
  juniper1 |    9 |       13
  juniper1 |   42 |       10
(8 rows)

test=*# select kabel_nr, string_agg(switch || ' ' || port::text, ' <-> 
') from kabel group by kabel_nr;
  kabel_nr |        string_agg
----------+---------------------------
        10 | cisco2 48 <-> juniper1 42
        14 | cisco1 6 <-> cisco2 6
        13 | cisco1 5 <-> juniper1 9
        12 | cisco1 4 <-> juniper1 8
(4 rows)

test=*#


Andreas

-- 
2ndQuadrant - The PostgreSQL Support Company.
www.2ndQuadrant.com