string aggregation 4000 +.

Hi all

I'm using Oracle 11.2.0.3.   I used LISTAGG to generate CSV values for names, but some of my values are greater than 4000 characters, it is throwing ORA-01489: result of concatenating string is too long.

I tried to XMLAGG allowing to solve. However, this special character space.  (e.g. & be escaped to & the equivalent of xml security)

for example

with t as

(SELECT ' has "PEOPLE ' as double fname)

Union of all the

SELECT ' BALL &' as double fname

Union of all the

Select "cat!"  as double fname)

Select rtrim (xmlagg (xmlelement (e, fname,',').extract('//text () ') order of fname).) GetClobVal(), ',') AS NAME of t

RETURNS: Has ' PEOPLE, BALL, & CAT!

Has ' PEOPLE EXPECTED, BALL & CAT!

Is it possible to disable escape everything using XMLAGG?

Is there any other method recommended to use aggregation of chain for more than 4000 characters?

Thank you very much!

The following would be the correct way to do it in your version.

Unescaping/cast the result as a CLOB will be XMLCast:

with t as)

Select ' has "PEOPLE ' as fname Union double all the

Select ' BALL &' as fname Union double all the

Select "cat!"  as double fname

)

Select rtrim)

(xmlcast)

XMLAGG)

XmlElement (e, fname |) ',')

order of the fname

)

as clob

)

, ','

) as name

t;

(in versions< 11.1,="" i="" would="" have="" used="" dbms_xmlgen.convert="" instead="" in="" order="" to="" unescape="" entity="">

Tags: Database

Similar Questions

  • string aggregation &gt; 4000 store in varchar2 (4000)

    Dear experts,

    I'm using Oracle 11.2.0.3. I have the table such as

    ID NAME

    1 xyz

    1 abc xyz

    1 xyz abc def

    2 abc

    2 xyz nmnx

    I want to aggregate them so that I have the aggregation of the NAME by ID

    as

    1XYZ xyz abc xyz abc def
    2ABC xyz nmnx

    I was able to do this using:

    WITH t
         AS (SELECT 1 AS id, 'xyz' AS name FROM DUAL
             UNION ALL
             SELECT 1, ' xyz abc' FROM DUAL
             UNION ALL
             SELECT 1, 'xyz abc  def' FROM DUAL
             UNION ALL
             SELECT 2, 'abc' FROM DUAL
             UNION ALL
             SELECT 2, 'xyz  nmnx' FROM DUAL)
    SELECT     id, RTRIM (XMLCAST (XMLAGG (XMLELEMENT (e, name, ' ') ORDER BY NULL) AS CLOB)) as agg_name
        FROM   t
    GROUP BY   id;
    
    

    However, if the length (agg_name) > 8 (for simplicity), then the name should be on a separate line with the same id

    1 xyz xyz

    1 abc def

    1 xyz ABC

    2 abc xyz

    2 nmnx

    token order is not important.

    Thanks for any help

    Above could be done using

    WITH t
         AS (SELECT 1 AS id, 'xyz' AS name FROM DUAL
             UNION ALL
             SELECT 1, ' xyz abc' FROM DUAL
             UNION ALL
             SELECT 1, 'xyz abc  def' FROM DUAL
             UNION ALL
             SELECT 2, 'abc' FROM DUAL
             UNION ALL
             SELECT 2, 'xyz  nmnx' FROM DUAL)
    SELECT     ID, LISTAGG (name) WITHIN GROUP (ORDER BY NULL) org_name
        FROM   (SELECT   ID,
                         name || ' ' name,
                         CEIL (SUM (len) OVER (PARTITION BY ID ORDER BY len) / 8) part,
                         len
                  FROM   (SELECT    /*distinct */  ID, x.name, LENGTH (x.name || ' ') len
                             FROM   t, XMLTABLE ('ora:tokenize(.," ")' PASSING ' ' || TRIM (name) COLUMNS name VARCHAR2 (4000) PATH '.') x
                            WHERE   x.name IS NOT NULL))
    GROUP BY   ID, part;
    
  • Query (connect / string aggregation)

    Hello
    Can someone give me an example of a query


    VAL ID
    --------
    2B
    1 B
    1 C


    Output: "A, B, C".

    Thank you.

    Published by: Bolev on February 3, 2010 17:10

    Hello

    Pivot generates separate columns for each val, and you should know how much they will be.

    String aggregation provides a single column and does not need to know how many vals there will be:

    WITH     got_rnum     AS
    (
         SELECT     id
         ,     val
         ,     ROW_NUMBER () OVER ( PARTITION BY  id
                                   ORDER BY          val
                           ) AS rnum
         FROM    table_x
    )
    SELECT     id
    ,     LTRIM ( SYS_CONNECT_BY_PATH (val, ',')
               , ','
               )     AS val_list
    FROM     got_rnum
    WHERE     CONNECT_BY_ISLEAF     = 1
    START WITH     rnum     = 1
    CONNECT BY     rnum     = PRIOR rnum + 1
         AND     id     = PRIOR id
    ;
    
  • Reg: String aggregation-

    Hi Experts,

    I have a scenario where my query returns 4 ranks as
    A
    B
    C
    D
    -Results: ' a ',' B', 'C', d '.

    How can I achieve this using the aggregation of String?

    I'm working on-
    SELECT CHR (39)||RTRIM(XMLAGG (XMLELEMENT (e, <table_column>||''',''')).EXTRACT ('//text()'),''',''')|| CHR (39)
      FROM <my_table>;
    but give...
    'QFND340B&apos;,&apos;QFND434&apos;,&apos;'
    Database details - 
    Oracle Database 10g Enterprise Edition Release 10.2.0.3.0 - 64bi
    PL/SQL Release 10.2.0.3.0 - Production
    CORE     10.2.0.3.0     Production
    TNS for Solaris: Version 10.2.0.3.0 - Production
    NLSRTL Version 10.2.0.3.0 - Production

    Try like this:

    select WM_CONCAT(''''||lvl||'''') from (
                              SELECT 'A' AS lvl FROM DUAL union all
                              SELECT 'B' AS lvl FROM DUAL union all
                              SELECT 'C' AS lvl FROM DUAL union all
                              SELECT 'D' AS lvl FROM DUAL
                              ) xx;
    
  • Parse qualified double quote delimited strings

    Hi all

    I have a few strings of data that I get I want to analyze. My problem is that I am not able to understand how I can analyze the fields that are double quote qualified ("19 999")?

    I played with the regex_substr, but I can't quite make what I want.

    I want to analyze the myString column in my example below in four areas:
    Product
    OnOrder
    OnHand
    TotalSold

    Here is an example... I've been playing with it for a bit, and I can't get it.
    WITH parseString AS (Select 
                            '"Grado, Headphones",123,2222,"19,999" ' myString
                         from dual
                         UNION ALL
                         Select 
                            'Audio Slave,222,3333,444' myString
                         from dual
                         
                                    )
    Select 
        myString
    from parseString
    Oracle Database 10 g Enterprise Edition release 10.2.0.3.0 - 64bi
    PL/SQL version 10.2.0.3.0 - Production
    CORE Production 10.2.0.3.0
    AMT for Linux: release 10.2.0.3.0 - Production
    NLSRTL Version 10.2.0.3.0 - Production


    Any suggestions greatly appreciated.

    S

    Published by: ScarpacciOne on April 22, 2010 15:24

    Hello

    As Centinul said, it would help a lot if you have published your desired results and explains how get you from the sample data.
    Here's something that can help, according to your specific needs:

    WITH     got_item_cnt     AS
    (
         SELECT     myString
         ,     1 + LENGTH (myString)
                - LENGTH (REPLACE (myString, ','))     AS item_cnt
         FROM     parsestring
    )
    ,     cntr     AS
    (
         SELECT     LEVEL     AS n
         FROM     dual
         CONNECT BY     LEVEL <= ( SELECT  MAX (item_cnt)
                           FROM    got_item_cnt
                         )
    )
    ,     comma_separated_values     AS
    (
         SELECT     i.myString
         ,     c.n
         ,     REGEXP_SUBSTR ( i.myString
                         , '[^,]+'
                         , 1
                         , c.n
                         )          AS value
         FROM     got_item_cnt     i
         JOIN     cntr          c     ON     c.n     <= i.item_cnt
    )
    ,     got_quote_cnt     AS
    (
         SELECT     myString
         ,     n
         ,     REPLACE (value, '"')     AS value
         ,     NVL ( SUM ( LENGTH (value)
                     - LENGTH (REPLACE (value, '"'))
                     ) OVER ( PARTITION BY  myString
                           ORDER BY      n
                           ROWS BETWEEN  UNBOUNDED PRECEDING
                             AND      1        PRECEDING
                            )
                  , 0
                  )          AS quote_cnt
         FROM     comma_separated_values
    )
    SELECT     myString
    ,     n
    ,     LTRIM ( SYS_CONNECT_BY_PATH (value, ',')
               , ','
               )          AS token
    FROM     got_quote_cnt
    WHERE     CONNECT_BY_ISLEAF     = 1
    START WITH     MOD (quote_cnt, 2)     = 0
    CONNECT BY     MOD (quote_cnt, 2)     = 1
         AND     myString     = PRIOR myString
         AND     n          = PRIOR n + 1
    ORDER BY     myString,     n
    ;
    

    Output of your sample data:

    MYSTRING                                N TOKEN
    -------------------------------------- -- --------------------
    "Grado, Headphones",123,2222,"19,999"   2 Grado, Headphones
    "Grado, Headphones",123,2222,"19,999"   3 123
    "Grado, Headphones",123,2222,"19,999"   4 2222
    "Grado, Headphones",123,2222,"19,999"   6 19,999
    Audio Slave,222,3333,444                1 Audio Slave
    Audio Slave,222,3333,444                2 222
    Audio Slave,222,3333,444                3 3333
    Audio Slave,222,3333,444                4 444
    

    The basic strategy is to divide myString to all the commas, then re - unite consecutive elements that are in double - quotes.
    You never said what version of Oracle are you uisng.
    This solution works in Oracle 10.1. REGEXP_COUNT, introduced in Oracle 11.1, would make it a bit simpler.

    Here is each subquery:
    got_item_count finds the number of elements by commas in each myString
    CNTR generates integers 1, 2, 3,... until the maximum number of elements by commas on line any
    comma_separated_values myString splits its different elements, ignoring the quotes at the moment. I assumed myString is unique. If this isn't the case, you need some other unique identifier for each row of parsestring.
    * quote_cnt had "notes how quotes took place in myString, each element not included.  If (and only if) this number is odd, then the element is really a continuation of a previous item, in other words, the comma that separates this point from the previous one was within quotation marks and therefore should not have divided myString.  I assumed that the double-quote character always indicates the grouping; It is never to be taken literally, and therefore the final results will never contain quotes.
    The main query string aggregation on consecutive points, bringing together all the elements that are weird quote_cnts with the last item previous had a same quote_cnt.

    Published by: Frank Kulash, 22 April 2010 23:33

  • Remove duplicates of LISTAGG

    Oracle 11 g 2 Server

    You need to remove from a call LISTAGG dups.

    create table ptpos_explanations (
        id    number ,
        fr_id number ,
        part_type_id   number ,
        position_id    number ,
        ptpos_list     varchar2(4000)
    );
    
    insert into ptpos_explanations
    values(182527,100, 10,1,'It seems like it was replaced by part_type(s)/position(s):10/1 from content: 100');
    
    
    insert into ptpos_explanations
    values(182527,100, 10,2,'It seems like it was replaced by part_type(s)/position(s):10/1 from content: 100');
    
    
    insert into ptpos_explanations
    values(182527,110, 20,1,'It seems like it was replaced by part_type(s)/position(s):20/2 from content: 100');
    
    insert into ptpos_explanations
    values(182527,110, 20,2,'It seems like it was replaced by part_type(s)/position(s):20/2 from content: 100');
    
    commit;
    
    select * from ptpos_explanations;
    
            ID      FR_ID PART_TYPE_ID POSITION_ID
    ---------- ---------- ------------ -----------
    PTPOS_LIST
    -----------------------------------------------------------------------------------------------------------------------------
        182527        100           10           1
    It seems like it was replaced by part_type(s)/position(s):10/1 from content: 100
    
        182527        100           10           2
    It seems like it was replaced by part_type(s)/position(s):10/1 from content: 100
    
        182527        110           20           1
    It seems like it was replaced by part_type(s)/position(s):20/2 from content: 100
    
        182527        110           20           2
    It seems like it was replaced by part_type(s)/position(s):20/2 from content: 100
    
    
    4 rows selected.
    
    

    Can I use LISTAGG on ptpos_list to refine the results.

    SELECT  id, part_type_id, LISTAGG(ptpos_list,', ') WITHIN GROUP(ORDER BY ptpos_list) recommendations
    FROM     ptpos_explanations
    GROUP BY id, part_type_id ;
    
    
            ID PART_TYPE_ID
    ---------- ------------
    RECOMMENDATIONS
    -----------------------------------------------------------------------------------------------------------------------------
        182527           10
    It seems like it was replaced by part_type(s)/position(s):10/1 from content: 100, It seems like it was replaced by part_type(
    s)/position(s):10/1 from content: 100
    
        182527           20
    It seems like it was replaced by part_type(s)/position(s):20/2 from content: 100, It seems like it was replaced by part_type(
    s)/position(s):20/2 from content: 100
    
    
    

    However, there are duplicates in the recommendations. I tried to wrap LISTAGG inside a REGEXP_REPLACE function, without much success.

    Here the final result, so I'm looking for:

            ID PART_TYPE_ID
    ---------- ------------
    RECOMMENDATIONS
    ---------------------------------------------------------------------------------
        182527           10
    It seems like it was replaced by part_type(s)/position(s):10/1 from content: 100
    
        182527           20
    It seems like it was replaced by part_type(s)/position(s):20/2 from content: 100
    
    
    

    EDIT: There are "n" lines for ptpos_list. So, we need to manage the many values of ptpos_list.

    Hello

    This is a feature annoying LISTAGG - you cannot use DIFFERENT with her.

    A workaround is to to a separate GROUP BY, or SELECT DISTINCT simply get the unique values, then call LISTAGG on these unique results, like this:

    WITH got_distinct_ptpos_list AS

    (

    SELECT id, part_type_id, ptpos_list

    OF ptpos_explanations

    GROUP BY id, part_type_id, ptpos_list

    )

    SELECT id, part_type_id

    LISTAGG (ptpos_list, ',')

    THE Group (ORDER BY ptpos_list) in the form of recommendations

    OF got_distinct_ptpos_list

    GROUP BY id, part_type_id

    ;

    LISTAGG isn't the only way to get the desired results.  See https://oracle-base.com/articles/misc/string-aggregation-techniques for other ways to string aggregation, including a user-defined aggregate function and SYS_CONNECT_BY_PATH.

    REGEXP_REPLACE should work, too.  What exactly have you tried?  (However, it is probably more effective to get the unique results immediately the bat, rather than producing more than you want and then by doing something more to get rid of the duplicates).

  • Addition of several xml using Insertxmlbefore in a single query. Help, please.

    Oracle version

    Oracle Database 11 g Enterprise Edition Release 11.2.0.4.0 - 64 bit Production

    PL/SQL Release 11.2.0.4.0 - Production

    CORE Production 11.2.0.4.0

    AMT for Linux: Version 11.2.0.4.0 - Production

    NLSRTL Version 11.2.0.4.0 - Production

    I have an xml where I need to insert a new xml basedupon the partyID.

    for each partyid in the need to xmoutput to insert representative data.

    with t as (
    select 1 source_no , xmltype('<report>
                       <partyReported partyId="1">
                         <name> TEST123</name>
                      </partyReported>
                      <partyReported partyId="2">
                         <name> TEST456</name>
                      </partyReported>
                   </report>')
                   xmloutput
                   from dual
                )
              ,
              t_member as (
               select 1 candidate_no, xmltype('<Representative>
                                           <name> rep123 </name>
                                  </Representative>') rep_xml from dual
              union all
              select 2 , xmltype('<Representative>
                                           <name> rep456 </name>
                                  </Representative>') from dual
            )
            , t_rep_member as(
            SELECT
              source_no
            , X.*
            , xmloutput
         FROM
              t
            , XMLTABLE ('/report/partyReported' passing xmloutput COLUMNS candidate_no INTEGER PATH '@partyId', candidate_idx FOR ordinality ) AS X
         )
         , all_data
         as (
         select sourcE_no, a.candidate_no, xmloutput, rep_xml from t_rep_member a, t_member b
         where a.candidate_no = b.candidate_no
         )
         select source_no, MergeRepXml(repxml(candidate_no,rep_xml,xmloutput)) final_xml from all_data
         group by sourCe_no;
    
    

    source_no, candidate_no, candidate_idx, xmloutput
    
    1    1    1    "<report>                   <partyReported partyId="1">                     <name> TEST123</name>                  </partyReported>                   <partyReported partyId="2">                     <name> TEST456</name>                  </partyReported>                </report>"
    1    2    2    "<report>                   <partyReported partyId="1">                     <name> TEST123</name>                  </partyReported>                   <partyReported partyId="2">                     <name> TEST456</name>                  </partyReported>                </report>"
    
    
    
    

    I solved this problem, but I get an error message when I try to do 1000 of them.

    ERROR:

    ORA-22813: value of the operand is greater than the limits of the system

    create or replace
    type RepXml as object( candidate_no number, rep_xml xmltype, output xmltype)
    /
    create or replace
    type RepXmltab as table of RepXml
    /
    
    
    
    
    
    create or replace
    type MERGEREP as object
    (
      -- string varchar2(4000),                  -- deleted
      val_table  RepXmltab ,           -- added
    
    
      static function ODCIAggregateInitialize
        ( sctx in out MERGEREP )
        return number ,
    
      member function ODCIAggregateIterate
        ( self  in out MERGEREP ,
          value in     RepXml
        ) return number ,
    
      member function ODCIAggregateTerminate
        ( self        in  MERGEREP,
          returnvalue out xmltype,
          flags in number
        ) return number ,
    
      member function ODCIAggregateMerge
        ( self in out MERGEREP,
          ctx2 in     MERGEREP
        ) return number
    );
    
    
    create or replace
    type body MERGEREP
    is
    
      static function ODCIAggregateInitialize
      ( sctx in out MERGEREP )
      return number
      is
      begin
    
         sctx := MERGEREP( repxmltab(null,null,null) );
         --val_table:= repxmltab(null);
    
        return ODCIConst.Success ;
    
      end;
    
      member function ODCIAggregateIterate
      ( self  in out MERGEREP ,
        value in     RepXml
      ) return number
      is
      begin
    
        self.val_table.extend;
        self.val_table(self.val_table.count) := value;
    
        return ODCIConst.Success;
      end;
    
      member function ODCIAggregateTerminate
      ( self        in  MERGEREP ,
        returnvalue out xmltype ,
        flags       in  number
      ) return number
      is
    
        v_data  xmltype ;
    
      begin
    
    v_data:= null;
    
    for x in (
         select candidate_no,rep_xml,output
          from   table(val_table)
          )
          loop
          v_data:= x.output;
    
          end loop;
               
       
        for x in
        (
    select candidate_no,rep_xml,output
          from   table(val_table)
          )
        loop
        select insertxmlbefore(v_data,'/report/partyReported[@partyId="'||x.candidate_no||'"]', x.rep_xml) into v_data from dual;
    null;
        end loop;
    
        returnValue := ( v_data) ;
    
    v_data:= null;
    
        return ODCIConst.Success;
    
      end;
    
      member function ODCIAggregateMerge
      ( self in out MERGEREP ,
        ctx2 in     MERGEREP
      ) return number
      is
      begin
    
        for i in 1 .. ctx2.val_table.count
        loop
          self.val_table.extend;
          self.val_table(self.val_table.count) := ctx2.val_table(i);
        end loop;
    
    
    
        return ODCIConst.Success;
    
            end;
      end;
    /
    
    create or replace
    function MergeRepXml
      ( input RepXml )
      return xmltype
      deterministic
      parallel_enable
      aggregate using MERGEREP
    ;
    /
    
    

    With my limited knowledge, I tried to write recursive with clause and achieve the expected results, but many lines are displayed.

    WITH
         t1 AS
         ( SELECT * FROM aps_extendedoutput WHERE source_no = 261177
         )
         --     select * from t1;
       , t AS
         (SELECT
              source_no
            , X.*
            , xmloutput
         FROM
              t1
            , XMLTABLE ('/report/role/partyReported' passing xmloutput COLUMNS candidate_no INTEGER PATH '@partyId', candidate_idx FOR ordinality ) AS X
         )
         --     select * from t;
       , all_data AS
         (SELECT
              /*+ materialize */
              t.candidate_no
            , rep_xml
            , source_no
            , t.candidate_no
            , xmloutput
         FROM
              t
            , aps_reppartyxml t_p
         WHERE
              t.candidate_no = t_p.candidate_no
         )
         --   select * from all_data;
       ,recursive_data(candidate_i, xmloutput, source_no, candidate_no) AS
         (SELECT
              1 candidate_i
              , xmloutput
            ,  source_no
            , 0 candidate_no
         FROM
             t1
         UNION ALL
         SELECT
              candidate_i + 1
              , insertxmlbefore(rd.xmloutput,'/report/role/partyReported[@partyId="'
              ||ad.candidate_no
              ||'"]', ad.rep_xml) xmloutput
            , ad.source_no
            , rd.candidate_no
         FROM
              all_Data ad
            , recursive_data rd
         WHERE
              ad.sourcE_no        = rd.source_no and
               candidate_i     < 3
         )
    SELECT *  FROM recursive_data;
    
    

    for example

    SQL> create table t_member as (
      2  select 1 candidate_no, xmltype('
      3                                rep123 
      4                      ') rep_xml from dual
      5  union all
      6  select 2 , xmltype('
      7                                rep456 
      8                      ') from dual
      9  );
    
    Table created.
    
    SQL>
    SQL> set long 5000
    SQL>
    SQL> with t (source_no, xmloutput) as (
      2    select 1
      3         , xmltype('
      4                      
      5                          TEST123
      6                      
      7                      
      8                          TEST456
      9                      
     10                   ')
     11    from dual
     12  )
     13  select t.source_no
     14       , xmlserialize(document
     15           xmlquery(
     16             'copy $d := .
     17              modify (
     18                for $i in $d/report/partyReported
     19                  , $j in fn:collection("oradb:/DEV/T_MEMBER")/ROW
     20                where $j/CANDIDATE_NO = $i/@partyId
     21                return insert node $j/REP_XML/* before $i
     22              )
     23              return $d'
     24             passing t.xmloutput
     25             returning content
     26           )
     27           indent
     28         ) as final_xml
     29  from t ;
    
     SOURCE_NO FINAL_XML
    ---------- --------------------------------------------------------------------------------
             1 
                 
                    rep123 
                 
                 
                    TEST123
                 
                 
                    rep456 
                 
                 
                    TEST456
                 
               
    
  • Need to concat lines to a single column

    Hi all

    Some examples of data.

    col1 col2 col3

    125 200 abc

    126 200 def

    127 200 IGS

    128 210 homeless

    129 rte 210

    130 211 uiy

    I need output like this.

    col2 col3

    200 abc, def, ghi

    210 homeless, rte

    uiy 211

    I need data with order of col1

    Could you please let me know how to build the query for this.

    Thanks in advance.

    Hello

    Use the function of aggregation LISTAGG, like this:

    SELECT col2

    LISTAGG (col3, ",") THE Group (ORDER BY col1) AS col3_list,

    FROM table_x

    GROUP BY col2

    ;

    This requires that you use Oracle 11.2 or greater.

    The generic term for this is the String aggregation.

    For more on the aggregation of the chain, including how to do this in earlier versions of Oracle, see ORACLE-BASE - String aggregation Techniques

  • SQL output format

    Dear all,

    Select emp_no in emp;

    my output is

    1

    2

    3

    4

    But I want that as output format

    1,2,3,4

    Could you please tell me which application should I write it to achieve.

    Concerning

    Rajat

    Hi, Rajat,

    This is called the aggregation of the chain and this page:

    ORACLE-BASE - String aggregation Techniques

    shows several ways to do so.  What sense does use?  It depends on your requitrements and your version of Oracle.  The LISTAGG built-in aggregate function is good for most purposes.

  • multiple lines to one line

    I have two tables

    Employee1:

    State of name ID

    1SAMPA
    2JOHNGA
    3STEVETX

    Employee2:

    State of name ID

    1MARKCA
    1JOHNNORTH CAROLINA
    1DARRENTX

    my query is:

    Select e1.emp_id, e2.emp_name in e1, e2 employee2 employee1

    where e1.emp_id = e2.emp_id

    result:

    1MARK
    1JOHN
    1DARREN

    but I want the result in a line like below:

    ID EMP_NAME

    1 MARK, JOHN, DARREN

    How can I combine the query with separated by commas.

    Thank you.

    Hello

    872918 wrote:

    using IAM

    Oracle Database 10 g Express Edition Release 10.2.0.1.0 - product

    PL/SQL Release 10.2.0.1.0 - Production

    This is an example of why you should always say what version of Oracle you are using, especially if this version is 9 years old.

    LISTAGG was new in Oracle 11.2.  In Oracle 10 (and more), you can use the SYS_CONNECT_BY_PATH, like this:

    WITH got_r_num AS

    (

    SELECT e1.emp_id

    e2.emp_name

    ROW_NUMBER () OVER (PARTITION BY e1.emp_id

    ORDER BY e2.emp_name

    ) AS r_num

    Of employee1 e1

    JOIN employee2 e2 ON e2.emp_id = e1.emp_id

    )

    SELECT emp_id

    LTRIM (SYS_CONNECT_BY_PATH (emp_name, ',')

    , ','

    ) AS all_enames

    OF got_r_num

    WHERE CONNECT_BY_ISLEAF = 1

    START WITH r_num = 1

    CONNECT BY r_num = r_num + 1

    AND emp_id = emp_id PRIOR

    ;

    Output of your sample data:

    EMP_ID ALL_ENAMES

    --------------- ---------------------------------------------

    1 DARREN, JOHN, MARK

    The generic name for this is the String aggregation.  For ways to string aggregation in different versions, see:

    ORACLE-BASE - String aggregation Techniques

  • need the output

    Hi all

    Player score

    10 001

    20 001

    20 002

    20 002

    10 001

    10 002

    20 003

    20 002

    10 001

    I want o/p in the select statement

    Player score

    001 10,20,10,10

    002 20,20,10,20

    20 003

    with t1 as

    (select the '001', '10' Score of double drive

    Union of all the

    Select the '001', '20' Score of double drive

    Union of all the

    Select the drive '002', '20' double Score

    Union of all the

    Select the drive '002', '20' double Score

    Union of all the

    Select the '001', '10' Score of double drive

    Union of all the

    Select the drive '002', '10' double Score

    Union of all the

    Select the drive '003', '20' double Score

    Union of all the

    Select the drive '002', '20' double Score

    Union of all the

    Select the '001', '10' Score of double drive)

    Select * from t1

    His pleasure... Try it for yourself... visit this link for examples:

    ORACLE-BASE - String aggregation Techniques http://www.Oracle-base.com/articles/10G/auditing-10gR2.php

  • Query Tunning

    Hi all

    I have the problem with the query. Can someone please help me with this...

    I have two tables namely xx_all and xx_lines_all

    xx_all has the following features

    flag of name alot_num

    1450 XXX Y

    1544 XYZ Y

    6598 ABC Y

    and xx_lines_all has the following features

    flag of field ext alot_num

    1450 1265 14 c Y

    1450 6541 15 c Y

    1450 7741 11A Y

    1544-1141-121 c Y

    1544 6541 14A Y

    I want to display the data as follows

    field of alot_num name ext

    1450 XXX 15547 14 c, 15 c, 11A

    1544 XYZ 7682 121C, 14A

    The ext above column is can be defined by group of sum (ext) (from table xx_lines_all) in alot_num (1265-6541-7741 = 15547 for alot_num = 1450)... And all the plots in xx_lines_all should come in one line for this particular alot_number as shown above, 14 c, 15 c, 11A to alot_num = 1450

    However, I am able to view the above format using functions but is at - it another way to display the format desired...

    Currently, there are in bulk records in the table causing delay to see the format for each line function call...

    I wrote functions just this one also check (if necessary)...

    Function to display the values of sum-

    CREATE OR REPLACE FUNCTION XX_EXT (P_ALOT_NUM NUMBER)

    RETURN NUMBER IS

    CURSOR CUR_PLOT_DAT

    IS

    PEX SELECT NVL (SUM (TO_NUMBER (ext)), 0)

    OF xx_lines_all

    WHERE LATEST_FLAG = 'Y '.

    AND ALOT_NUM = P_ALOT_NUM;

    REC_PLOT_DAT CUR_PLOT_DAT % ROWTYPE;

    L_EXT_AREA NUMBER: = 0;

    L_AREA NUMBER: = 0;

    BEGIN

    CUR_PLOT_DAT OPEN;

    LOOP

    EXTRACT THE CUR_PLOT_DAT IN L_EXT_AREA;

    WHEN THE OUTPUT CUR_PLOT_DAT % NOTFOUND;

    L_AREA: = L_EXT_AREA;

    END LOOP;

    CLOSE CUR_PLOT_DAT;

    RETURN (NVL(L_AREA,0));

    END XX_EXT;

    To view the track on one line...

    CREATE OR REPLACE FUNCTION XX_PLOT (P_ALOT_NUM NUMBER)

    RETURN VARCHAR2 IS

    CURSOR CUR_PLOT_DAT

    IS

    SELECT FIELD PEX

    OF XX_LINES_ALL

    WHERE LATEST_FLAG = 'Y '.

    AND ALOT_NUM = P_ALOT_NUM;

    REC_PLOT_DAT CUR_PLOT_DAT % ROWTYPE;

    L_SECTION VARCHAR2 (5000);

    L_PLOT VARCHAR2 (1000);

    BEGIN

    CUR_PLOT_DAT OPEN;

    LOOP

    EXTRACT THE CUR_PLOT_DAT IN REC_PLOT_DAT;

    WHEN THE OUTPUT CUR_PLOT_DAT % NOTFOUND;

    L_SECTION: = L_SECTION | REC_PLOT_DAT. PEX | «, » ;

    END LOOP;

    CLOSE CUR_PLOT_DAT;

    RETURN (NVL (L_SECTION,' '));

    END XX_PLOT;

    Please help me...

    Thanks in advance...

    for 11g:

    Select t1.alot_num, t1.name, sum (t2.ext), listagg (t2.plot) within the Group (order by rownum)

    of xx_all t1

    Join the t2 xx_lines_all

    On t1.alot_num = t2.alot_num

    -I forgot to group of

    T1.alot_num group, t1.name

    less than 11g watch "String aggregation"

    Re: 4. How can I convert rows to columns?

    ----

    Ramin Hashimzade

    Post edited by: Ramin Hashimzadeh

  • The same ID and Message of a group line

    ID MESSAGES
    1On the method of calculation error
    1T_RATE cannot be null
    2On the method of calculation error
    2Length is greater than 10

    ID MESSAGES
    1Error on the method of calculation, T_RATE cannot be null
    2Error on the method of calculation, the length was greater than 10

    How to group the same id and message together? Thank you

    Hello

    This is called String aggregation.  Here's a way to do it:

    SELECT id

    LISTAGG (messages, ",") THE Group (ORDER BY NULL) as messages

    FROM table_x

    GROUP BY id

    ;

    Other means, see

    ORACLE-BASE - String aggregation Techniques

  • help required on table transpose

    DATA IN THE TABLE:

    ST IC OC         

    I have 18 2

    20 4 K

    10 1 L

    where ST, IC and OC are names of columns and I want data from the table format below. Please help,

    I K L         

    IC 18 20 10

    OC       2    4      1

    Hello

    Visit these pages to learn how to rotate the data:

    http://forums.Oracle.com/forums/thread.jspa?MessageID=4347279�

    http://forums.Oracle.com/forums/thread.jspa?MessageID=3662005

    If you don't know not set an upper limit for the number of columns in the result, swing requires dynamic SQL and I recommend you rather use String aggregation , where the output is a column of the great string, format to watch as separate columns.

    If you get stuck, post your best attempt, as well as some examples of data (CREATE TABLE and only relevant columns, INSERT statements) and the desired results from these data.

    Always say what version of Oracle you are using (for example, 11.2.0.2.0).

    See the forum FAQ https://forums.oracle.com/message/9362002#9362002

  • LISTTAG functionlity in Oracle 10 G

    Hello world
    I use Oracle Database 10 g Enterprise Edition Release 10.2.0.1.0 - 64 bit.
    Please look at my request and help out me. Will it work for Oracle 10 g?
    SELECT WEST_TEST_ID, LISTAGG(WEST_TEST_CODE, ',')
    WITHIN GROUP (ORDER BY WEST_TEST_CODE) AS test_codes
    FROM test_code_conversion
    GROUP BY WEST_TEST_ID;
    The error I'm getting is ORA-00923 (From keyword not found)
    Kind regards
    BS2012.

    No, you must be on the version 11.2 of the database (and beyond) If you want to use LISTAGG.
    Other techniques can be found here:
    http://www.Oracle-base.com/articles/Misc/string-aggregation-techniques.php (try instead the STRAGG)

Maybe you are looking for