ORACLE/SQL: wm_concat & order by

前端 未结 6 919
梦如初夏
梦如初夏 2020-12-19 18:17

I\'m using oracle 11 (not sure about the exact version, but since LISTAGG doesn\'t work, I suppose it\'s not release 2) through ODBC and crystal reports 2008.

Here

6条回答
  •  一整个雨季
    2020-12-19 19:17

    LISTAGG was introduced in 11g Release 2.

    Therefore, in Oracle version prior to 11g where LISTAGG is not supported, you could use ROW_NUMBER() and SYS_CONNECT_BY_PATH functions.

    See Oracle String Aggregation Techniques

    SELECT task_card,
      LTRIM(MAX(SYS_CONNECT_BY_PATH(code,','))
      KEEP (DENSE_RANK LAST ORDER BY curr),',') AS zones
      FROM   (SELECT task_card,
                    code,
                    ROW_NUMBER() OVER (PARTITION BY fruit ORDER BY code) AS curr,
                    ROW_NUMBER() OVER (PARTITION BY fruit ORDER BY code) -1 AS prev
             FROM   table_name)
      GROUP BY task_card
      CONNECT BY prev = PRIOR curr AND task_card= PRIOR task_card
     START WITH curr = 1;
    

    NOTE

    Never use WM_CONCAT since it is an undocumented feature and it has been removed from 12c version.

    Any application which has had been relying on wm_concat function will not work once upgraded to 12c. Since, it has been removed. See Why not use WM_CONCAT function in Oracle?

    SQL> select banner from v$version where rownum = 1;
    
    BANNER
    ----------------------------------------------------------------------------
    Oracle Database 12c Enterprise Edition Release 12.1.0.1.0 - 64bit Production
    
    SQL> SELECT object_name
      2  FROM dba_objects
      3  WHERE owner='WMSYS'
      4  AND object_name LIKE 'WM\_%' ESCAPE '\';
    
    OBJECT_NAME
    ----------------------------------------------------------------------------
    WM_REPLICATION_INFO
    WM_RDIFF
    WM_PERIOD
    WM_PERIOD
    WM_OVERLAPS
    WM_MEETS
    WM_LESSTHAN
    WM_LDIFF
    WM_INTERSECTION
    WM_INSTALLATION
    WM_GREATERTHAN
    WM_EVENTS_INFO
    WM_ERROR
    WM_ERROR
    WM_EQUALS
    WM_DDL_UTIL
    WM_DDL_UTIL
    WM_CONTAINS
    WM_COMPRESS_BATCH_SIZES
    WM_COMPRESSIBLE_TABLES
    
    20 rows selected.
    
    SQL>
    

    You will receive an “invalid identifier” error:

    SQL> SELECT banner FROM v$version;
    
    BANNER
    ----------------------------------------------------------------------------
    Oracle Database 12c Enterprise Edition Release 12.1.0.1.0 - 64bit Production
    PL/SQL Release 12.1.0.1.0 - Production
    CORE    12.1.0.1.0      Production
    TNS for 64-bit Windows: Version 12.1.0.1.0 - Production
    NLSRTL Version 12.1.0.1.0 - Production
    
    SQL> SELECT deptno, wm_concat(ename) FROM emp;
    SELECT deptno, wm_concat(ename) FROM emp
                   *
    ERROR at line 1:
    ORA-00904: "WM_CONCAT": invalid identifier
    

    Therefore, there is no point relying on an undocumented feature which is no more made available in latest versions.

提交回复
热议问题