简体   繁体   中英

SQL Listagg function - error

I am having trouble with my LISTAGG function. I keep getting the error ORA-00937: not a single group function . 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( [,]) 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). 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"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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