简介
‘Loop group by’即,分组处理数据,通过group快捷进行数据的分组处理,使用这个方法,可以更加便利地对需要分组的数据进行处理。
效果展示
代码展示
REPORT test01.
"定义数据
TYPES:BEGIN OF gty_out,
name TYPE string,
role TYPE string,
type TYPE string,
age TYPE i,
END OF gty_out.
DATA:gt_out TYPE TABLE OF gty_out,
gs_out LIKE LINE OF gt_out.
DATA:gt_out2 LIKE gt_out.
"准备实验数据
gt_out = VALUE #(
( name = '实验1号' role = '类型1' type = '属1' age = 23 )
( name = '实验2号' role = '类型2' type = '属1' age = 24 )
( name = '实验3号' role = '类型1' type = '属2' age = 21 )
( name = '实验4号' role = '类型3' type = '属1' age = 25 )
( name = '实验5号' role = '类型2' type = '属2' age = 28 )
( name = '实验6号' role = '类型3' type = '属2' age = 23 )
( name = '实验7号' role = '类型2' type = '属1' age = 20 )
).
"分组
LOOP AT gt_out INTO gs_out GROUP BY ( key1 = gs_out-role
* key2 = gs_out-type
index = GROUP INDEX "生成分组索引行
size = GROUP SIZE ) "生成分组成员数
DESCENDING ASSIGNING FIELD-SYMBOL(<tab1>). "将分组按照降序排列
"将分组的数据进行处理(添加到表便于展示)
LOOP AT GROUP <tab1> ASSIGNING FIELD-SYMBOL(<wa1>).
APPEND <wa1> TO gt_out2.
ENDLOOP.
"展示数据
cl_demo_output=>write( | INDEX:{ <tab1>-index } ,SIZE:{ <tab1>-size }| ) .
cl_demo_output=>write( gt_out2 ).
CLEAR gt_out2.
ENDLOOP.
cl_demo_output=>display( ).