string_agg没有函数匹配给定的名称

Sar*_*rit 20 postgresql

我有关系数据库,并希望使用,string_agg()因为它似乎适合我的需要.

我想要 :

product_id | quiz_id
-----------+----------
         1 | 1,6
         2 | 2,7
         3 | 3,8
         4 | 4
Run Code Online (Sandbox Code Playgroud)

这是我的数据库.

    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
Run Code Online (Sandbox Code Playgroud)

我的尝试:
参考文件. 如何通过'查询连接PostgreSQL'组中的字符串字段的字符串? 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.
Run Code Online (Sandbox Code Playgroud)


Postgres版本:
PostgresApp 9.4.4.1

更新:
@ code-monk它仍然是错误的.

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.
Run Code Online (Sandbox Code Playgroud)

问题:
我的查询有什么问题

Abh*_*hek 52

试试这个:

select product_id , string_agg(quiz_id::character varying, ',' order by lastmodified) 
from quiz group by product_id;
Run Code Online (Sandbox Code Playgroud)

String_agg函数仅适用于String值,因为quiz_id是整数,所以得到错误.

我已将其转换为character varying并添加了group by以便明智地对数据产品ID进行分组.

SQL小提琴示例:http://sqlfiddle.com/#!15/9dafe/1

  • 或者只是“quiz_id::varchar” (5认同)