2016-01-26 44 views
3

我有選擇,其中我想只有一行收集:的LISTAGG(如在下面的代碼)這對ORA-00937:「不是一個單組基函數」

被限制
  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; 

回答

4

您錯誤地使用聚合LISTAGG而不是分析函數。添加一個OVER子句給LISTAGG,你很好。

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子句,以利用其聚集的版本了。)