Skip to content

ABAP Keyword Documentation →  ABAP − Reference →  Processing External Data →  ABAP Database Access →  ABAP SQL →  ABAP SQL - Operands and Expressions →  ABAP SQL - SQL Expressions sql_exp →  sql_exp - sql_agg →  ABAP SQL - Examples of Aggregate Expressions 

SQL Expression, Aggregate Function string_agg

This example demonstrates the aggregate function string_agg.

Other versions: 7.31 | 7.40 | 7.54

Source Code

    DELETE FROM demo_expressions.
    INSERT demo_expressions FROM TABLE @( VALUE #(
      ( id = '5' char1 = 'nowhere' )
      ( id = '3' char1 = 'this' )
      ( id = '1' char1 = 'everybody' )
      ( id = '4' char1 = 'is' )
      ( id = '2' char1 = 'knows' ) ) ).

    SELECT STRING_AGG( char1,' ' )
           FROM demo_expressions
           INTO TABLE @DATA(unordered).
    cl_demo_output=>write( unordered ).

    SELECT STRING_AGG( char1,' ' ORDER BY id ASCENDING )
           FROM demo_expressions
           INTO TABLE @DATA(ordered_ascending).
    cl_demo_output=>write( ordered_ascending ).

    SELECT STRING_AGG( char1,' ' ORDER BY id DESCENDING )
           FROM demo_expressions
           INTO TABLE @DATA(ordered_descending).
    cl_demo_output=>write( ordered_descending ).

    cl_demo_output=>display( ).

Description

Uses the aggregate function STRING_AGG as an aggregate expression in the SELECT list with different sorts.