【问题标题】:error raise when use within GROUP with string_agg in postgresql在 GROUP 中与 postgresql 中的 string_agg 一起使用时引发错误
【发布时间】:2019-09-20 07:09:02
【问题描述】:

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

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

我在和 atrelation 有桌子。 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

【问题讨论】:

    标签: postgresql string-aggregation


    【解决方案1】:

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

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

    【讨论】:

    • 我做了但引发了错误:在“来自”或附近的语法错误选择 atrelation.id 作为 id, string_agg(at.description, 'ANQ' ORDER BY atrelation.id) 在 GROUP 内
    • select atrelation.id, string_agg(at.description, ' and ' ORDER BY atrelation.id ) as tag1 within GROUP from at, atrelation where atrelation.id = atrelation.atid group by atrelation.id order by atrelation.id desc;
    • @aqh: 你需要删除WITHIN GROUP 使用它,就像我展示的那样。
    • 感谢@a_horse_with_no_name。我想你必须在这个好答案之后选择一个好名字
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-07-29
    • 1970-01-01
    • 2021-08-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多