sap abap ‘loop at group‘ 用法

*& Insert data to table lt_ztquo2
lt_ztquo2 = VALUE #( ( object_id = 001 process_type = 'AA' created_at = '20230910' created_by = 'lisurangg' description = 'OK' item_id = '01' product_id = '0014' quantity = 100 unii = 'PCS' )
                     ( object_id = 001 process_type = 'AA' created_at = '20230910' created_by = 'lisurangg' description = 'OK' item_id = '02' product_id = '0014' quantity = 100 unii = 'PCS' )
                     ( object_id = 002 process_type = 'AE' created_at = '20230910' created_by = 'lisurangg' description = 'OK' item_id = '01' product_id = '0015' quantity = 300 unii = 'PCS' )
                     ( object_id = 002 process_type = 'AP' created_at = '20230910' created_by = 'lisurangg' description = 'OK' item_id = '02' product_id = '0013' quantity = 300 unii = 'PCS' )
                     ( object_id = 003 process_type = 'AP' created_at = '20230910' created_by = 'lisurangg' description = 'OK' item_id = '01' product_id = '0014' quantity = 30  unii = 'PCS' )
                     ( object_id = 003 process_type = 'AE' created_at = '20230910' created_by = 'lisurangg' description = 'OK' item_id = '02' product_id = '0014' quantity = 45  unii = 'PCS' )
                     ( object_id = 004 process_type = 'AP' created_at = '20230910' created_by = 'lisurangg' description = 'OK' item_id = '01' product_id = '0019' quantity = 45  unii = 'PCS' )
                     ( object_id = 005 process_type = 'TA' created_at = '20230910' created_by = 'lisurangg' description = 'OK' item_id = '01' product_id = '0909' quantity = 12  unii = 'PSC' )
                     ( object_id = 006 process_type = 'TA' created_at = '20230910' created_by = 'lisurangg' description = 'OK' item_id = '01' product_id = '0909' quantity = 13  unii = 'PSC' )
                     ( object_id = 007 process_type = 'TA' created_at = '20230910' created_by = 'lisurangg' description = 'OK' item_id = '01' product_id = '0909' quantity = 100 unii = 'PSC' ) ).

先准备数据

目标:把相同object_id的数据的quantity汇总

使用方法 loop at group

TYPES: BEGIN OF lv_sum,
  object_id TYPE ztquoth_lsr-object_id,
  quantity  TYPE ztquoti_lsr-quantity,
       END OF lv_sum.
DATA: lt_sum TYPE TABLE OF lv_sum,
      wa_sum LIKE LINE OF lt_sum.

*& Group data by the same object_id
LOOP AT lt_ztquo2 INTO wa_ztquo2 GROUP BY (
                       object_id = wa_ztquo2-object_id
                       size  = GROUP SIZE
                       index = GROUP INDEX )
  ASCENDING ASSIGNING FIELD-SYMBOL(<group>).

*& Clear the variable
  CLEAR gv_su.

*& Calculate the same of quantity by group
  LOOP AT GROUP <group> ASSIGNING FIELD-SYMBOL(<wa_ztquo2>).
    gv_su = gv_su + <wa_ztquo2>-quantity.
  ENDLOOP.

wa_sum-object_id = <group>-object_id.
wa_sum-quantity = gv_su.
APPEND wa_sum TO lt_sum.
CLEAR wa_sum.

*& Output the sum of quantity for each group
  WRITE: / | object_id: { <group>-object_id } |.
  WRITE: |Quantity Sum: { gv_su }|.
ENDLOOP.

从此博主学习来的方法

LOOP GROUP BY 分组循环的使用方法小栗子_寒武青锋的博客-CSDN博客

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

李苏冉

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值