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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33717624/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 06:06:33  来源:igfitidea点击:

string_agg No function matches the given name

postgresql

提问by Sarit

I have relational database and would like to use string_agg()since it seems fit to my need.

I want :

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

我想要 :

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

Here is my database.

这是我的数据库。

    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

My attempt :
Refer to document. 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

我的尝试:
参考文档。 如何在 PostgreSQL 'group by' 查询中连接字符串字段的字符串?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


Postgres 版本:
PostgresApp 9.4.4.1

Update :
@code-monk It still error.

更新:
@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.

Question :
What is wrong with my query?

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

回答by Abhishek

Try this:

尝试这个:

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

String_aggfunction works with String values only ,You are getting the error because quiz_idis integer.

String_agg函数仅适用于字符串值,您收到错误,因为它quiz_id是整数。

I have converted it to character varyingand added group by for grouping the data product ID wise.

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

SQL Fiddle Example:http://sqlfiddle.com/#!15/9dafe/1

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