06/29/14 For a Better Future 1
LISTAGG
A Closer Look at what is
ListAgg?
06/29/14 For a Better Future 2
Introduction
 To have a better understanding of
LISTAGG built-in feature.
 By Kai Liu, Sy...
06/29/14 For a Better Future 3
Topics of Discussion
 What is LISTAGG?
 What is String Aggregation?
 Prior to Oracle 11g...
06/29/14 For a Better Future 4
What is LISTAGG?
 LISTAGG built-in feature was released in Oracle
11g Release 2 a function...
What is LISTAGG Cont?
 ORDER BY is Mandatory.
 If the order is not relevant to the results then you can use a constant
e...
06/29/14 For a Better Future 6
What is String Aggregation?
 String aggregation is the grouping and
concatenation of rows ...
What is String Aggregation?
 SELECT deptno
,LISTAGG(ename, ',') WITHIN GROUP (ORDER
BY ename) AS employees
FROM emp
GROUP...
06/29/14 For a Better Future 8
Prior to Oracle 11g Rel
2.
• LISTAGG is similar to WM_CONCAT and STRAGG, but
the difference...
How to use LISTAGG as an
analytic function?
 It can be converted to an analytic function by
adding the OVER() clause.
 S...
How to use LISTAGG as an
analytic function Cont?
DEPTNO ENAME HIREDATE EMPLOYEES
---------- ---------- ----------- -------...
06/29/14 For a Better Future 11
Work Example
 I had a project building a process that uses the
LISTAGG function in a gene...
Work Example Cont.
 Real Example:
 The code snipped below retrieved the Column Names for the
string in the dynamic sql:
...
Work Example Cont.
 The snippet query to retrieve values using the v_market_list provide
from above query:
v_work_market ...
Work Example Cont.
 The end result was a string of comma delimited
sequences for my purposes. But, you can see
that with ...
06/29/14 For a Better Future 15
Summary?
 The summary is that LISTAGG is here to stay and
is documented and support by Or...
06/29/14 For a Better Future 16
Thank You
 Q&A?
Upcoming SlideShare
Loading in …5
×

What is Listagg?

375 views

Published on

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
375
On SlideShare
0
From Embeds
0
Number of Embeds
4
Actions
Shares
0
Downloads
0
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

What is Listagg?

  1. 1. 06/29/14 For a Better Future 1 LISTAGG A Closer Look at what is ListAgg?
  2. 2. 06/29/14 For a Better Future 2 Introduction  To have a better understanding of LISTAGG built-in feature.  By Kai Liu, Systems Analyst at Chicago Stock Exchange.
  3. 3. 06/29/14 For a Better Future 3 Topics of Discussion  What is LISTAGG?  What is String Aggregation?  Prior to Oracle 11g Rel 2.  How to use LISTAGG as an analytic function?  Work Example.  Summary
  4. 4. 06/29/14 For a Better Future 4 What is LISTAGG?  LISTAGG built-in feature was released in Oracle 11g Release 2 a function that will enable us to perform string aggregation natively.  The LISTAGG function has the following syntax  LISTAGG( [,]) WITHIN GROUP (ORDER BY ) [OVER (PARTITION BY )]  The following are mandatory:  the column or expression to be aggregated;  the WITHIN GROUP keywords;  the ORDER BY clause within the grouping.
  5. 5. What is LISTAGG Cont?  ORDER BY is Mandatory.  If the order is not relevant to the results then you can use a constant expression such as NULL.  SELECT deptno , LISTAGG(ename, ',') WITHIN GROUP (ORDER BY NULL) AS employees FROM emp GROUP BY deptno; DEPTNO EMPLOYEES ---------- ------------------------------------------------------------ 10 CLARK,KING,MILLER 20 ADAMS,FORD,JONES,SCOTT,SMITH 30 ALLEN,BLAKE,JAMES,MARTIN,TURNER,WARD *Alphabetically seems to appear to be the default behavior when using a constant. Results using LISTAGG have a maximum size of VARCHAR2(4000). 06/29/14 For a Better Future 5
  6. 6. 06/29/14 For a Better Future 6 What is String Aggregation?  String aggregation is the grouping and concatenation of rows of data into a single row per group. For Example:  DEPTNO ENAME  --------- ----------  10 CLARK  10 KING  10 MILLER  20 ADAMS  20 FORD  20 JONES  Using the Emp table from the Scott user:
  7. 7. What is String Aggregation?  SELECT deptno ,LISTAGG(ename, ',') WITHIN GROUP (ORDER BY ename) AS employees FROM emp GROUP BY deptno;  DEPTNO EMPLOYEES --------- ------------------------- 10 CLARK,KING,MILLER 20 ADAMS,FORD,JONES 30 ALLEN,BLAKE,JAMES,MARTIN,TURNER,WARD  We can see that the employee names have been grouped and concatenated by comma into a single row. 06/29/14 For a Better Future 7
  8. 8. 06/29/14 For a Better Future 8 Prior to Oracle 11g Rel 2. • LISTAGG is similar to WM_CONCAT and STRAGG, but the difference been that it is a documented and supported function by oracle. This function is a part of Oracle 11g Release 2 version. Additional features of this function have over previous functions are: 1)Can select your own separator. The default value is NULL. 2) Can get the ordered list by ordering the column. 3) Supported by Oracle, can be used in Production system. 4) Much faster than WM_CONCAT and little better than STRAGG function.
  9. 9. How to use LISTAGG as an analytic function?  It can be converted to an analytic function by adding the OVER() clause.  SELECT deptno, ename, hiredate, LISTAGG (ename, ',') WITHIN GROUP (ORDER BY hiredate) OVER (PARTITION BY deptno) AS employees FROM emp; 06/29/14 For a Better Future 9
  10. 10. How to use LISTAGG as an analytic function Cont? DEPTNO ENAME HIREDATE EMPLOYEES ---------- ---------- ----------- ------------------------------------- 10 CLARK 09/06/1981 CLARK,KING,MILLER 10 KING 17/11/1981 CLARK,KING,MILLER 10 MILLER 23/01/1982 CLARK,KING,MILLER 20 SMITH 17/12/1980 SMITH,JONES,FORD,SCOTT,ADAMS 20 JONES 02/04/1981 SMITH,JONES,FORD,SCOTT,ADAMS 20 FORD 03/12/1981 SMITH,JONES,FORD,SCOTT,ADAMS 20 SCOTT 19/04/1987 SMITH,JONES,FORD,SCOTT,ADAMS 20 ADAMS 23/05/1987 SMITH,JONES,FORD,SCOTT,ADAMS 06/29/14 For a Better Future 10
  11. 11. 06/29/14 For a Better Future 11 Work Example  I had a project building a process that uses the LISTAGG function in a generic way.  The process needed to create a list of values based on the Column_Name from the table provided to be used in dynamic sql to return correct records based on criteria and list of values provided. The columns had a naming convention of SEQ in name to allow its additional columns sequences needed for new markets.  The codes snipped below is simplified to help illustrate the contents of the discussion of using the LISTAGG.
  12. 12. Work Example Cont.  Real Example:  The code snipped below retrieved the Column Names for the string in the dynamic sql: SELECT REPLACE (LISTAGG ('NVL(' || COLUMN_NAME || ',0)', ',') WITHIN GROUP (ORDER BY COLUMN_NAME), ',NVL','||'',''||NVL') AS markets INTO V_MARKET_LISTS FROM ALL_TAB_COLS WHERE table_name = 'QUOTE_DATA' AND OWNER = 'KLIU' AND column_name LIKE '%SEQ%' ORDER BY COLUMN_NAME; 06/29/14 For a Better Future 12
  13. 13. Work Example Cont.  The snippet query to retrieve values using the v_market_list provide from above query: v_work_market := 'SELECT ‘|| v_markets_list || ' from QUOTE_DATA where SYMBOL in ‘’’ || P_SYMBOL || ‘’’; -- Fetch all the market sequence for timeframe. OPEN cur_qm_seq_ref FOR v_work_markets; FETCH cur_qm_seq_ref INTO v_market_seq_list; CLOSE cur_qm_seq_ref; Execute this query to return the comma delimited values to retrieve correct records to filter and returned.  06/29/14 For a Better Future 13
  14. 14. Work Example Cont.  The end result was a string of comma delimited sequences for my purposes. But, you can see that with some creativity you can do some cool things. 06/29/14 For a Better Future 14
  15. 15. 06/29/14 For a Better Future 15 Summary?  The summary is that LISTAGG is here to stay and is documented and support by Oracle. Other option were more workarounds and not recommended for future use. I have found the LISTAGG to be a good string aggregation technique that all developers to be aware of for the right situation. Clearly the performance is faster for the new LISTAGG function over previous techniques used before.
  16. 16. 06/29/14 For a Better Future 16 Thank You  Q&A?

×