Oracle LISTAGG Function: Turning Rows into a Single String

Oracle LISTAGG Function

Oracle LISTAGG Function

Imagine you manage an Oracle E-Business Suite (EBS) system, and you need to display all item categories assigned to a specific inventory item in a single row. The Oracle LISTAGG function makes this easy. It aggregates multiple row values into a single string, separated by a chosen delimiter.

Why is Oracle LISTAGG Function Useful?

Real-world scenarios where LISTAGG is beneficial include:

  • Generating Reports: In Oracle EBS, procurement teams might need a consolidated list of suppliers providing a particular material.
  • Order Management: In Order Management (OM), you might want to display all discounts applied to an order in a single field.
  • Inventory Management: When analyzing inventory items, you may need a comma-separated list of item categories for better classification.

How Does Oracle Listagg Work?

The basic syntax of Oracle Listagg is given below:

Oracle Listagg function syntax
LISTAGG(column_name, 'delimiter') WITHIN GROUP (ORDER BY column_name)
Syntax of Oracle Listagg function
  • column_name: The column whose values you want to concatenate.
  • delimiter (optional): The separator (e.g., comma, semicolon, space).
  • WITHIN GROUP (ORDER BY column_name): Specifies the order of concatenation.

Oracle LISTAGG Example: Item Categories

Let’s consider Oracle LISTAGG Example with inventory items with item categories table:

ITEM_IDCATEGORY_NAME
1001Electronics
1001Mobile
1001Accessories
1002Furniture
1002Office

To list all categories for item 1001 in a single row:

Example Oracle LISTAGG
SELECT  LISTAGG(MC.performance tuning in oracle apps r12, ', ') 
       WITHIN GROUP (ORDER BY MC.CONCATENATED_SEGMENTS) AS category_list
  FROM MTL_ITEM_CATEGORIES mic,
       MTL_CATEGORIES_KFV MC
 WHERE MIC.inventory_item_id=1001
   AND MC.CATEGORY_ID=MIC.CATEGORY_ID;
Oracle LISTAGG Example

Handling Large Strings with ON OVERFLOW TRUNCATE

If the string exceeds the maximum length, Oracle will throw an error. Let’s assume you want to list down all the column name of a Oracle ERP database table using listagg function. If you keep the concatenated column name into a field with fewer characters it will through error.

Adding ON OVERFLOW TRUNCATE to the LISTAGG function makes it completely clear that an error is expected when an overflow happens. Let’s review below Oracle LISTAGG Example:

Oracle LISTAGG Example with overflow
SELECT listagg(column_name, ','  ON OVERFLOW TRUNCATE '...') WITHIN group 
       (ORDER BY column_name) AS columns
  FROM all_tab_columns
 WHERE table_name = upper('fnd_conc_req_summary_v')
GROUP BY table_name;
overflow Oracle LISTAGG Example
Oracle LISTAGG Example with overflow Output

If the result is too long, Oracle truncates it and appends “…”.

Key Considerations

  • Data Types: Convert numeric or date values to strings using TO_CHAR().
  • Performance: For large datasets, LISTAGG can be resource-intensive.
  • Oracle Versions: ON OVERFLOW TRUNCATE is available from Oracle 12c Release 2 onwards.

By using LISTAGG in Oracle EBS, you can streamline reporting and data analysis, making information more accessible and structured.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.