简体   繁体   English

SQL Listagg函数 - 错误

[英]SQL Listagg function - error

I am having trouble with my LISTAGG function. 我的LISTAGG功能有问题。 I keep getting the error ORA-00937: not a single group function . 我一直收到错误ORA-00937:不是单个组功能 I have googled this error but it's still not quite clear on what's wrong. 我已经搜索了这个错误,但仍然不清楚错误是什么。

Scenario : I have a table of segments. 场景 :我有一个段表。 A segment can have multiple people assigned to them (one to many). 一个细分可以分配多个人(一对多)。 I need my ouput / report to show the segment number in one column and a list of the users in the other. 我需要我的输出/报告来显示一列中的段号和另一列中的用户列表。

Query : 查询

    select fcs.nld_flood_control_segment_id
         , fcs.fc_segment_name
         , fcs.fc_segment_abbrev_name
         , LISTAGG(ps.first_name|| ' ' ||ps.last_name, ', ') within group (ORDER BY fcs.nld_flood_control_segment_id, ps.last_name) "ListOfReps"
      from nld_flood_control_segments   fcs
         , nld_fc_segment_person_xref   xr
         , persons                      ps
     where fcs.nld_flood_control_segment_id = :P1_id
       and :P1_id                           = xr.nld_flood_control_segment_id 
       and xr.person_id                     = ps.person_id
  order by nld_flood_control_segment_id asc
         ;

Any help would be greatly appreciated. 任何帮助将不胜感激。 Thanks in advance. 提前致谢。

The LISTAGG function has the following syntax structure: LISTAGG函数具有以下语法结构:

LISTAGG( [,]) WITHIN GROUP (ORDER BY ) [OVER (PARTITION BY )]

LISTAGG is an aggregate function that can optionally be used as an analytic (ie the optional OVER() clause). LISTAGG是一个聚合函数,可以选择用作分析(即可选的OVER()子句)。 The following elements are mandatory: 以下要素是强制性的:

the column or expression to be aggregated;
the WITHIN GROUP keywords;
the ORDER BY clause within the grouping.

Example: 例:

LISTAGG(ename, ',') WITHIN GROUP (ORDER BY ename) AS employees

Try the following to see if it will provide what you need. 请尝试以下操作,看看它是否能满足您的需求。

LISTAGG(ps.first_name|| ' ' ||ps.last_name, ',') within group (ORDER BY ps.first_name|| ' ' ||ps.last_name) "ListOfReps"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM