listagg function

Upload: ravi-kumar-pasupula

Post on 03-Jun-2018

213 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/12/2019 ListAgg Function

    1/3

    LISTAGG

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

    LISTAGGis a built-in function that enables us to perform string

    aggregation natively. String aggregation is a popular technique, and there are several methods

    available.

    hat is string aggregation!

    String aggregation is simply the grouping and concatenation of multiple ros of data into a

    single ro per group. "or e#ample, consider the folloing resultset$

    %&'T() &(A*&

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

    + LA/

    + /I(G

    + *ILL&

    0 A%A*S 0 ")%

    0 1)(&S

    2ith string aggregation, this resultset ould be grouped 3by %&'T()4 as follos$

    %&'T() AGG&GAT&%5&(A*&S

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

    + LA/,/I(G,*ILL&

    0 A%A*S,")%,1)(&S

    2e can see that the employee names have simply been grouped and

    concatenated into a single column 3values are delimited by comma4 per group. As stated, there are

    several techniques available to perform this aggregation 3references are provided at the end of this

    article4, but the ne LISTAGG function ma6es it much easier, as e ill see belo.

    The LISTAGG function has the folloing synta# structure$

    LISTAGG3 7,84 2IT9I( G):' 3)%& ;< 4 7)=& 3'ATITI)( ;< 48

    LISTAGG is an aggregate function that can optionally be used as an analytic 3i.e. the

    optional )=&34 clause4. The folloing elements are mandatory$

    the column or e#pression to be aggregated>

    the 2IT9I( G):' 6eyords>

    the )%& ;< clause ithin the grouping.

    2e ill no see some e#amples of the function belo.

    2e ill begin ith a simple e#ample that aggregates the employee names

    for each department in the &*' table, using a comma as delimiter.

    S?L@ S&L&T deptno 0 , LISTAGG3ename, ,4 2IT9I( G):' 3)%& ;< ename4 AS employees

    B ")* emp

  • 8/12/2019 ListAgg Function

    2/3

    C G):' ;

    %&'T() &*'L)

  • 8/12/2019 ListAgg Function

    3/3

    &*'() &(A*& %&'T() &(A*&S5LIST

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

    EEF0 LA/ + LA/,/I(G,*ILL&

    EFB /I(G + LA/,/I(G,*ILL&

    EBC *ILL& + LA/,/I(G,*ILL&

    EFEH A%A*S 0 A%A*S,")%,1)(&S,S)TT,S*IT9E0 ")% 0 A%A*S,")%,1)(&S,S)TT,S*IT9

    EDHH 1)(&S 0 A%A*S,")%,1)(&S,S)TT,S*IT9

    EEFF S)TT 0 A%A*S,")%,1)(&S,S)TT,S*IT9

    EBH S*IT9 0 A%A*S,")%,1)(&S,S)TT,S*IT9

    EC ALL&( B ALL&(,;LA/&,1A*&S,*ATI(,T:(&,2A%

    EHF ;LA/& B ALL&(,;LA/&,1A*&S,*ATI(,T:(&,2A%

    E 1A*&S B ALL&(,;LA/&,1A*&S,*ATI(,T:(&,2A%

    EHDC *ATI( B ALL&(,;LA/&,1A*&S,*ATI(,T:(&,2A%

    EFCC T:(& B ALL&(,;LA/&,1A*&S,*ATI(,T:(&,2A%

    ED0+ 2A% B ALL&(,;LA/&,1A*&S,*ATI(,T:(&,2A%

    +C ros selected

    emember that hen use an analytic function, e do not lose any ros from

    our resultset$ rather e see the results of the aggregation on every source ro 3depending on the

    analytic function and the orderingindoing clauses4. 2e can clearly see this above. The string

    aggregation for each department is available on every corresponding ro.