Hallo zusammen,
ich möchte meine Kunden bzgl. SEPA anschreiben. Ein Kunde kann mehrere Bankverbindungen bei uns hinterlegt haben, ich möchte aber dass der Kunde nur einen Brief mit allen Bankverbindungen erhält.
Folgendes Beispiel:
postgres=# select * from bankverbindung;
kundennr | kontonr | bankleitzahl | iban | bic
------------+------------+--------------+------------------------+-------------
0000000001 | 0000589468 | 80050000 | DE48648648416846841358 | DEF84384138
0000000001 | 4444489468 | 80050000 | DE48648648444854785458 | DEF84384138
0000000001 | 4468894684 | 80050000 | DE48648555555554785458 | DEF84384138
0000000021 | 4488544896 | 10000000 | DE48648564546484444412 | DEF85846848
0000555021 | 8885547896 | 10000000 | DE41448568999456845622 | DEF85846848
(5 Zeilen)
Bei dem Kunden mit der Nr. 0000000001 hätte ich nun gerne als Ergebnis nur eine Zeile, aber dafür mit mehreren Spalten. Ich muss hier ein paar Hundert Kunden anschreiben, einige haben nur eine Bankverbindung, andere haben bis zu 10 hinterlegt.
Den FAQ-Beitrag hab ich mir bereits durchgelesen, aber dazu muss ja der Inhalt der Spalte fix sein.
Crosstab hab ich mir in der Doku mal angeschaut, aber entweder stell ich mich zu dumm, oder in der Doku passt was nicht.
Folgendes steht in der Doku (http://www.postgresql.org/docs/8.4/static/tablefunc.html):
CREATE TABLE ct(id SERIAL, rowid TEXT, attribute TEXT, value TEXT);
INSERT INTO ct(rowid, attribute, value) VALUES('test1','att1','val1');
INSERT INTO ct(rowid, attribute, value) VALUES('test1','att2','val2');
INSERT INTO ct(rowid, attribute, value) VALUES('test1','att3','val3');
INSERT INTO ct(rowid, attribute, value) VALUES('test1','att4','val4');
INSERT INTO ct(rowid, attribute, value) VALUES('test2','att1','val5');
INSERT INTO ct(rowid, attribute, value) VALUES('test2','att2','val6');
INSERT INTO ct(rowid, attribute, value) VALUES('test2','att3','val7');
INSERT INTO ct(rowid, attribute, value) VALUES('test2','att4','val8');
SELECT *
FROM crosstab(
'select rowid, attribute, value
from ct
where attribute = ''att2'' or attribute = ''att3''
order by 1,2')
AS ct(row_name text, category_1 text, category_2 text, category_3 text);
row_name | category_1 | category_2 | category_3
----------+------------+------------+------------
test1 | val2 | val3 |
test2 | val6 | val7 |
(2 rows)
Das gleiche ergibt bei mir:
postgres=# select * from ct;
id | rowid | attribute | value
----+-------+-----------+-------
1 | test1 | att1 | val1
2 | test1 | att2 | val2
3 | test1 | att3 | val3
4 | test1 | att4 | val4
5 | test2 | att1 | val5
6 | test2 | att2 | val6
7 | test2 | att3 | val7
8 | test2 | att4 | val8
(8 Zeilen)
postgres=# SELECT *
postgres-# FROM crosstab(
postgres(# 'select rowid, attribute, value
postgres'# from ct
postgres'# where attribute = ''att2'' or attribute = ''att3''
postgres'# order by 1,2')
postgres-# AS ct(row_name text, category_1 text, category_2 text, category_3 tex
t);
FEHLER: Funktion crosstab(unknown) existiert nicht
ZEILE 2: FROM crosstab(
^
TIP: Keine Funktion stimmt mit dem angegebenen Namen und den Argumenttypen über
ein. Sie müssen möglicherweise ausdrückliche Typumwandlungen hinzufügen.
postgres=#
Jemand eine Idee für mich?