在postgresql中在GROUP中与string_agg一起使用时引发错误

时间:2019-09-20 07:09:02

标签: postgresql string-aggregation

我在Postgresql 10中的查询在GROUP子句中使用时引发错误

  

错误:函数string_agg(字符变化,未知,整数)确实   不存在

我有at和atrelation表。 at具有唯一的ID和描述,而atrelation存储具有相关at和交易行ID的代码的多个交易。例如

id为6的产品行的列名称标签的值为service5%和contco4.5%

id为5的产品行的标签值为service5%

我需要显示2行,即第6行和第5行。

第5行显示列标记值'service5%和contco4.5%'

第6行显示列标记值“ service5%”

select  atrelation.id, 
        string_agg(at.description, ' and ' ) within GROUP (ORDER BY atrelation.id )  as tag1    
from  at, atrelation    
where atrelation.id = atrelation.atid   
group by atrelation.id    
order by atrelation.id desc;

上述查询引发以下错误

ERROR:  function string_agg(character varying, unknown, integer) does not exist
LINE 1: select atrelation.purchase_order_line_id as id, string_agg(a...
                                                        ^
HINT:  No function matches the given name and argument types. You might need to add explicit type casts.
********** Error **********

ERROR: function string_agg(character varying, unknown, integer) does not exist
SQL state: 42883
Hint: No function matches the given name and argument types. You might need to add explicit type casts.
Character: 49

1 个答案:

答案 0 :(得分:0)

您需要将ORDER BY放入函数调用中,并删除WITHIN GROUP部分:

string_agg(at.description, ' and ' ORDER BY atrelation.id)  as tag1