You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用SQL LISTAGG合并行时报错“不是GROUP BY函数”如何解决?

错误原因

SQL中使用GROUP BY子句时,SELECT后面所有没有被聚合函数包裹的列,必须全部放入GROUP BY子句中。你当前的SQL语句里,SELECT除了聚合函数LISTAGG和指定的分组列person_id外,还有tbc.idattribute、tbc.idclient、tbc.idclientattribute、tbc.idattributetype、tbl.vcdescription这几个列,既没有被聚合函数包裹,也没有加入GROUP BY子句,因此触发了“不是GROUP BY函数”的报错。

解决方案

可以根据你的业务需求选择以下两种修改方案:

  • 方案1:保留维度列分组
    如果你需要保留查询结果中每个person_id下上述额外列的不同取值行,直接把这些非聚合列全部加到GROUP BY子句中即可,修改后代码如下:
select
tbc.idattribute,
tbc.idclient,
tbc.idclientattribute,
tbc.idattributetype,
tbl.vcdescription,
LISTAGG(tb.vclongdescription, '; ') WITHIN GROUP(ORDER BY tbc.idclient) "test_consent",
person_id
from
tbclientattribute tbc, tblookupheader tbl, tbclient, tblookupdetail tb
where tbc.idattributetype = tbl.idlookupheader (+)
and tbclient.idclient = tbc.idclient (+)
and tbc.idattribute = tb.idlookupdetail(+)
group by person_id, tbc.idattribute, tbc.idclient, tbc.idclientattribute, tbc.idattributetype, tbl.vcdescription
order by person_id
  • 方案2:仅按person_id分组
    如果你要求每个person_id只返回一行结果,不需要保留额外列的不同维度值,就需要对这些额外列用合适的聚合函数包裹,比如用MAX()/MIN()取极值,或者根据需求用LISTAGG合并,示例代码如下:
select
MAX(tbc.idattribute) idattribute,
MAX(tbc.idclient) idclient,
MAX(tbc.idclientattribute) idclientattribute,
MAX(tbc.idattributetype) idattributetype,
MAX(tbl.vcdescription) vcdescription,
LISTAGG(tb.vclongdescription, '; ') WITHIN GROUP(ORDER BY tbc.idclient) "test_consent",
person_id
from
tbclientattribute tbc, tblookupheader tbl, tbclient, tblookupdetail tb
where tbc.idattributetype = tbl.idlookupheader (+)
and tbclient.idclient = tbc.idclient (+)
and tbc.idattribute = tb.idlookupdetail(+)
group by person_id
order by person_id

内容的提问来源于stack exchange,提问作者nelson

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.25 05:15:03