2015-11-15 8 views
12

Ich habe relationale Datenbank und möchte string_agg() verwenden, da es scheint, dass es zu meinem Bedarf passt.

Ich möchte:
string_agg Keine Funktion entspricht dem Vornamen

product_id | quiz_id 
-----------+---------- 
     1 | 1,6 
     2 | 2,7 
     3 | 3,8 
     4 | 4 

Hier meine Datenbank ist.

select quiz_id , product_id, lastmodified from dugong.quiz; 
quiz_id | product_id |   lastmodified   
---------+------------+------------------------------- 
     1 |   1 | 2015-11-11 14:46:55.619162+07 
     2 |   2 | 2015-11-11 14:46:55.619162+07 
     3 |   3 | 2015-11-11 14:46:55.619162+07 
     4 |   4 | 2015-11-11 14:46:55.619162+07 
     5 |   5 | 2015-11-11 14:46:55.619162+07 
     6 |   1 | 2015-11-11 14:46:55.619162+07 
     7 |   2 | 2015-11-11 14:46:55.619162+07 
     8 |   3 | 2015-11-11 14:46:55.619162+07 

Mein Versuch:
Siehe dokumentieren. How to concatenate strings of a string field in a PostgreSQL 'group by' query? http://www.postgresql.org/docs/current/static/sql-expressions.html#SYNTAX-AGGREGATES

select product_id , string_agg(quiz_id, ',' order by lastmodified) from dugong.quiz; 
ERROR: function string_agg(integer, unknown) does not exist 
LINE 1: select product_id , string_agg(quiz_id, ',' order by lastmod... 
          ^
HINT: No function matches the given name and argument types. You might need to add explicit type casts. 


Postgres Version:
PostgresApp 9.4.4.1

Update:
@ code-Mönch Es ist immer noch Fehler.

select product_id , string_agg(quiz_id::int, ',' order by lastmodified) from dugong.quiz; 
ERROR: function string_agg(integer, unknown) does not exist 
LINE 1: select product_id , string_agg(quiz_id::int, ',' order by la... 
          ^
HINT: No function matches the given name and argument types. You might need to add explicit type casts. 

Frage:
Was mit meiner Anfrage falsch ist?

Antwort

26

Versuchen Sie folgendes:

select product_id , string_agg(quiz_id::character varying, ',' order by lastmodified) 
from quiz group by product_id; 

String_agg Funktion arbeitet mit String-Werte nur, Sie erhalten den Fehler, da quiz_id ganze Zahl ist.

Ich habe es character varying umgewandelt und Gruppe hinzugefügt von für die Daten Produkt-ID weise Gruppierung.

SQL Fiedel Beispiel: http://sqlfiddle.com/#!15/9dafe/1

+0

Oh! Vielen Dank. Ich habe nie etwas über technische Hintergründe gewusst. – Sarit

+0

Stackoverflow mir vor ein grünes Häkchen setzen warten 4 Minuten lassen. Bitte warte einen Moment. :) – Sarit

+0

Okay ... akzeptiere dann :) –

Verwandte Themen