What’s New In ABAP – Series 01, Episode 10 – Loop AT .. Group By.

Nagendra Babu
Authored by

LOOP AT .. GROUP BY:

The addition of ‘GROUP BY ‘with ‘LOOP AT’, groups the rows of the internal table and executes a loop across the groups. Alright, I don’t know about you when I read this I have no clue what does it do. Let me get you to the actual point, first have look at the syntax of LOOP AT…GROUP BY.

General Syntax:
LOOP AT itab result [ cond ] GROUP BY group_key [ ASCENDING|DESCENDING [AS TEXT] ]  
[WITHOUT MEMBERS ] [ group_result ].
.
.
[ LOOP AT GROUP …
.
.
ENDLOOP. ]
.
.
ENDLOOP.

In the above syntax ‘group_key’ is specified as below.

… key | ( key1 = dobj1  key2 = dobj2 …   [gs = GROUP SIZE] [gi = GROUP INDEX] ) …

Do no get overwhelmed by the whole syntax 😀 ,  it is not as complex as it looks. lets get into the details.

Syntax Explanation:

result: Result holds the work area.

[cond]: You can specify the ‘WHERE’ condition just like the old way.

GROUP BY group_key: in ‘group_key’ component ‘gs‘ will have the no.of lines contain in the group for each loop and ‘gi‘ will have the number of group for each loop depending on Ascending/Descending.

[ASCENDING|DESCENDING]: sorts the groups by the group key in ascending or descending order before the group loop is executed inside this loop.

WITHOUT MEMBERS: This addition constructs groups but there is no access to the rows of the groups in the group loop.

Okay enough with details, let dive into the examples, the fun part.

got to home page?

Following example clears all of your doubts:

TYPES:
BEGIN OF ty_employees,
position(10TYPE c,
name         TYPE char10,
emp_name(20TYPE c,
END   OF ty_employees.

TYPEStt_employees TYPE STANDARD TABLE OF  ty_employees WITH EMPTY KEY.

–> Populating internal table for our example.

DATA(t_emp_infoVALUE tt_employees(
(  position ='p001'  name 'Manager'   emp_name 'Amanada' )
(  position ='p001'  name 'Executive' emp_name 'Sandy'   )
(  position ='p001'  name 'Executive' emp_name 'Sam'     )
(  position ='p004'  name 'Fin.mang'  emp_name 'Nagen'   )
(  position ='p005'  name 'Fin.mang'  emp_name 'Tom'     )
).

–>Do not worry about this line, just used for output.

DATA(outcl_demo_output=>new).

–> Lets group the positions and display them.

LOOP AT t_emp_info INTO DATA(waGROUP BY position wa-position group_records GROUP SIZE group_no GROUP INDEX )
ASCENDING
ASSIGNING FIELD-SYMBOL(<lfs_group_info>).

Here <lfs_group_info> contains the members data (if you do not need members data specify ‘WITHOUT MEMBERS’). And the ‘GROUP SIZE’ stores the no.of records in each group, ‘GROUP_INDEX’ stores no.of groups.

out->begin_sectionGroup No{ <lfs_group_info>-group_no } | && | no.of records in each group { <lfs_group_info>-group_records } | ). “Used for headings in output

–> Using FOR or by LOOP pass the member values to members itab. Here I am using FOR iteration expression.

DATA(membersVALUE tt_employeesFOR  wa2 IN GROUP <lfs_group_info> wa2 
out->writemembers ).
out->end_section).
ENDLOOP.

–> Display the output.

out->display).

Output:

Try on your system still if you have any doubts. 🙂

Complete Code:

TYPES:
BEGIN OF ty_employees,
position(10TYPE c,
name         TYPE char10,
emp_name(20TYPE c,
END   OF ty_employees.

TYPEStt_employees TYPE STANDARD TABLE OF  ty_employees WITH EMPTY KEY.

DATA(t_emp_infoVALUE tt_employees(
(  position ='p001'  name 'Manager'   emp_name 'Amanada' )
(  position ='p001'  name 'Executive' emp_name 'Sandy'   )
(  position ='p001'  name 'Executive' emp_name 'Sam'     )
(  position ='p004'  name 'Fin.mang'  emp_name 'Nagen'   )
(  position ='p005'  name 'Fin.mang'  emp_name 'Tom'     )
).

DATA(outcl_demo_output=>new).

LOOP AT t_emp_info INTO DATA(waGROUP BY position wa-position group_records GROUP SIZE group_no GROUP INDEX )
ASCENDING
ASSIGNING FIELD-SYMBOL(<lfs_group_info>).

out->begin_sectionGroup No{ <lfs_group_info>-group_no } | && | no.of records in each group { <lfs_group_info>-group_records } | ).

DATA(membersVALUE tt_employeesFOR  wa2 IN GROUP <lfs_group_info> wa2 
out->writemembers ).
out->end_section).
ENDLOOP.

out->display).

End of series, got to home page

Got any queries? Please go ahead to the comment section and post your query. Happy to help :).