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

error raise when use within GROUP with string_agg in postgresql

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

ERROR: function string_agg(character varying, unknown, integer) does not exist

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

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

ID 为 5 的产品行具有标签值 service5%

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

第 5 行显示列标记值 'service5% and 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

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

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