Oracle Database 11g企业版11.2.0.4.0版中的STRAGG - 64位生产

时间:2016-05-20 07:10:41

标签: sql oracle oracle11g

我想在此环境中使用STRAGG功能:Oracle Database 11g企业版11.2.0.4.0版 - 64位生产版,PL / SQL版本11.2.0.4.0 - 生产版,CORE 11.2.0.4.0生产版,TNS for Linux:Version 11.2.0.4.0 - Production,NLSRTL Version 11.2.0.4.0 - Production:

我有一个名为 V_USER_ROLE_DESC

的视图
LOGIN   DESC      

joanet  BS in ANDROID.C.3
joanet  DOB in ANDROID.C.3
joanet  DO in ANDROID.C.3
joanet  BS in ANDROID.C.4
joanet  UA in ANDROID.C.4
joanet  OV in ANDROID.C.4
joanet  OI in ANDROID.C.4
joanet  DO in ANDROID.C.4
joanet  DHoU in ANDROID.C.4
joanet  AOP in ANDROID.C.4

执行此查询

select
  login ,
  sys.STRAGG( UNIT_ROLE_DESC || ' - ' ) as string
from
  V_USER_ROLE_DESC
where login = 'joanet'
group by
  login
;

结果如下:

joanet  BS in ANDROID.C.3 - DOB in ANDROID.C.3 - DO in ANDROID.C.3 - BS in ANDROID.C.4 - UA in ANDROID.C.4 - OV in ANDROID.C.4 - OI in ANDROID.C.4 - DO in ANDROID.C.4 - DHoU in ANDROID.C.4 - AOP in ANDROID.C.4 - 

但没有where子句......

   select
      login ,
      sys.STRAGG( UNIT_ROLE_DESC || ' - ' ) as string
    from
      V_USER_ROLE_DESC
    --where login = 'joanet'
    group by
      login
    ;

这是结果:

...
colau   DOB in ANDROID.D.2 - 
joanet  DOB in ANDROID.D.2 - 
sisa    DOB in ANDROID.D.2 - 
...

使用

SELECT login ,listagg(unit_role_desc,' - ') WITHIN GROUP (ORDER BY unit_role_desc) AS STRING FROM v_user_role_desc WHERE 1=1 GROUP BY LOGIN;

我有这个错误

ORA-01489:字符串连接的结果太长

1 个答案:

答案 0 :(得分:2)

我建议使用LISTAGG而不是STRAGG。 LISTAGG是优越的,因为它允许指定聚合项目的顺序。

SELECT login
      ,listagg(unit_role_desc,' - ')  WITHIN GROUP (ORDER BY unit_role_desc) AS STRING
  FROM v_user_role_desc
 WHERE 1=1
-- AND login = 'joanet'
 GROUP BY LOGIN;

另见一个工作示例:

WITH
 v_user_role_desc AS (
 SELECT 'sisa' login
        ,'BS in ANDROID.C.3' UNIT_ROLE_DESC 
    FROM dual
  UNION ALL
 SELECT 'sisa' login
        ,NULL UNIT_ROLE_DESC 
    FROM dual
  UNION ALL
 SELECT 'joanet' login
        ,'BS in ANDROID.C.3' UNIT_ROLE_DESC 
    FROM dual
  UNION ALL
  SELECT 'joanet' login
        ,'DOB in ANDROID.C.3' UNIT_ROLE_DESC 
    FROM dual
  UNION ALL
  SELECT 'joanet' login
        ,'DO in ANDROID.C.3' UNIT_ROLE_DESC 
    FROM dual
  UNION ALL
  SELECT 'joanet' login
        ,'BS in ANDROID.C.4' UNIT_ROLE_DESC 
    FROM dual
  UNION ALL
  SELECT 'joanet' login
        ,'UA in ANDROID.C.4' UNIT_ROLE_DESC 
    FROM dual
  UNION ALL
  SELECT 'joanet' login
        ,'OV in ANDROID.C.4' UNIT_ROLE_DESC 
    FROM dual
  UNION ALL
  SELECT 'joanet' login
        ,'OI in ANDROID.C.4' UNIT_ROLE_DESC 
    FROM dual
  UNION ALL
  SELECT 'joanet' login
        ,'DO in ANDROID.C.4' UNIT_ROLE_DESC 
    FROM dual
  UNION ALL
  SELECT 'joanet' login
        ,'DHoU in ANDROID.C.4' UNIT_ROLE_DESC 
    FROM dual
  UNION ALL
  SELECT 'joanet' login
        ,'AOP in ANDROID.C.4' UNIT_ROLE_DESC 
    FROM dual) 
SELECT login
      ,listagg(unit_role_desc,' - ')  WITHIN GROUP (ORDER BY unit_role_desc) AS STRING
  FROM v_user_role_desc
 WHERE 1=1
-- AND login = 'joanet'
 GROUP BY LOGIN;