ORA-00937:“不是单组组功能”

时间:2016-01-26 08:44:39

标签: sql oracle

我有选择,我只想收集一行: listagg(如下面的代码所示),必须加以限制

  1. 最多40行(因为varchar长度有限)
  2. 所有行的总计数 "P1 = 'Y'" (P1 flag can be only Y or null)
  3. 我在两个选项中单独做了,但我希望它很好而且流畅。
    不幸的是,这段代码给了我ORA-00937: "not a single-group group function"。有什么建议吗?

    ls_list := '';
    select LISTAGG(A || '.' || B||'('||to_char(C)||')',',')
             WITHIN GROUP (ORDER BY B) as Segments
             ,count(*) over (partition by P1)    --this count is the problem
       into ls_list, ll_total
       from xmltable('DBStatus/Body/Segments/Segment' passing gx_status
                          columns A VARCHAR2(30) path '@A'
                                 ,B VARCHAR2(30) path '@B'
                                 ,C NUMBER path '@C'
                                 ,P1 CHAR(1) path '@P1'
                    )
       where P1 = 'Y'
             and
             rownum <= 40;
    

1 个答案:

答案 0 :(得分:4)

您错误地使用了聚合LISTAGG而不是分析函数。向LISTAGG添加OVER子句,你没事。

select 
  listagg(a || '.' || b||'('||to_char(c)||')',',')
    within group (order by b) 
    over (partition by p1) as segments,
  count(*) 
    over (partition by p1)
...

(或者从COUNT中删除OVER子句,以便也使用其聚合版本。)