原文链接:https://blog.csdn.net/lmf496891416/article/details/111317377
1.格式:
LOOP AT 内表 INTO DATA(工作区) GROUP BY ( 字段 = 工作区-字段 size = GROUP SIZE "分组组数-- index = GROUP INDEX ) "分组的组序号 ASCENDING ASSIGNING FIELD-SYMBOL(<组>). LOOP AT GROUP <组> ASSIGNING FIELD-SYMBOL(<指针>). "对指针进行操作,和普通的loop循环相似 ENDLOOP. ENDLOOP.
2.简而言之:就是第一个loop循环是把内表进行分组,第二loop循环是把组里的数据循环读出来进行修改判断等操作。
3.可以替代AT END OF /AT NEW OF。
4.具体实例-复制就能用
TYPES: BEGIN OF ty_employee, name TYPE char30, role TYPE char30, age TYPE i, sex TYPE char10, END OF ty_employee, ty_employee_t TYPE STANDARD TABLE OF ty_employee WITH KEY name. "对内表进行赋值" DATA(gt_employee) = VALUE ty_employee_t( ( name = 'John' role = 'ABAP guru' age = 34 sex = 'MAN' ) ( name = 'Alice' role = 'FI Consultant' age = 42 sex = 'WOMAN' ) ( name = 'Barry' role = 'ABAP guru' age = 54 sex = 'WOMAN' ) ( name = 'Mary' role = 'FI Consultant' age = 37 sex = 'WOMAN' ) ( name = 'Arthur' role = 'ABAP guru' age = 34 sex = 'MAN' ) ( name = 'Mandy' role = 'SD Consultant' age = 64 sex = 'MAN' ) ). DATA: gv_tot_age TYPE i, gv_avg_age TYPE decfloat34. "通过角色进行分组" LOOP AT gt_employee INTO DATA(ls_employee) GROUP BY ( role = ls_employee-role size = GROUP SIZE "分组组数" index = GROUP INDEX ) "分组的组序号" ASCENDING ASSIGNING FIELD-SYMBOL(<group>). CLEAR: gv_tot_age. WRITE: / | 组序号: { <group>-index } 角色: { <group>-role WIDTH = 15 }| & | 这个组有几条数据: { <group>-size } |. "loop循环组,将组里的数据读出来" LOOP AT GROUP <group> ASSIGNING FIELD-SYMBOL(<ls_member>). gv_tot_age = gv_tot_age + <ls_member>-age. WRITE: /13 <ls_member>-name . ENDLOOP. gv_avg_age = gv_tot_age / <group>-size. WRITE: / |组平均年龄: { gv_avg_age }|. SKIP. ENDLOOP.
实例最终结果图: