【问题标题】:ORA-00937: "not a single-group group function"ORA-00937: "不是单组群函数"
【发布时间】:2016-01-26 08:44:38
【问题描述】:

我有 select,其中我只想收集一行:listagg(如下面的代码) 必须限制在

  1. 最多 40 行(因为 varchar 长度有限)
  2. 所有行的总数"P1 = 'Y'" (P1 flag can be only Y or null)


我在两次选择中分别进行了操作,但我希望它既美观又流畅。
不幸的是,这段代码给了我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;

【问题讨论】:

    标签: sql oracle


    【解决方案1】:

    您错误地使用了聚合 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子句,也可以使用它的聚合版本。)

    【讨论】:

      猜你喜欢
      • 2011-08-01
      • 1970-01-01
      • 2022-01-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-16
      • 2020-05-10
      相关资源
      最近更新 更多