ABAP 新语法 GROUPBY

作者:ABAP猿 约 5 分钟阅读 更新日期:2025-03-02 1 年前更新 标签:ABAP, Data Engineering, 开发, 数据工程
ABAP 新语法 GROUPBY - 封面图
ABAP 新语法 GROUPBY - 封面图
图片来源:微信公众号(如有侵权请联系删除)
ABAP 新语法 GROUPBY - 封面图
ABAP 新语法 GROUPBY - 封面图

Venture outside your comfort zone! The rewards are worth it.

勇敢踏出舒适圈去冒险吧!那些回报绝对值得。

ABAP 新语法 GROUPBY - 封面图
ABAP 新语法 GROUPBY - 封面图

1

程序效果

ABAP 新语法 GROUPBY - 封面图
ABAP 新语法 GROUPBY - 封面图

2

源码分享

ABAP 新语法 GROUPBY - 封面图
ABAP 新语法 GROUPBY - 封面图

*&\----------------------------------------------------------------------

*& 13\. GROUP BY 关键字 理解:内表分组

*&\----------------------------------------------------------------------

*& 语法:LOOP AT itab result [cond] GROUP BY group_key

*& [ASCENDING|DESCENDING [AS TEXT]]

*& [WITHOUT MEMBERS]

*& [group_result].

*& ...

*& LOOP AT GROUP ...

*& ...

*& ENDLOOP.]

*& ...

*& ENDLOOP.

*&\----------------------------------------------------------------------

TYPES

BEGIN OF gty_student ,

name ( 10 ) TYPE c , "姓名

gender TYPE c , "性别

age TYPE i , "年龄

id ( 10 ) TYPE n , "学生ID

class ( 10 ) TYPE c , "班级

score TYPE p DECIMALS 1 , "成绩

level ( 10 ) TYPE c , "级别

END OF gty_student ,

gty_t_student TYPE TABLE OF gty_student WITH EMPTY KEY .

DATA ( gt_student ) = VALUE gty_t_student ( LET pre = 'Student'

random = cl_abap_random_int => create ( seed = CONV # ( sy \- timlo

) min = 0 max = 100 ) IN

FOR n = 1 UNTIL n > 10

( id = n

name = |{ pre } { n }|

gender = COND # ( WHEN n MOD 2 = 0 THEN '男' ELSE '女' )

age = 10 \+ n

class = |{ n }班|

score = random -> get_next ( ) ) ).

SORT gt_student BY score DESCENDING .

LOOP AT gt_student INTO DATA ( gs_student ) WHERE score > 60 GROUP BY

gs_student \- gender INTO DATA ( grp ).

"分组处理

DATA ( lt_grp_stu ) = VALUE gty_t_student ( FOR ls_grp IN GROUP grp

( ls_grp ) ).

ENDLOOP .

*------------------------------------------------------------------------------*

LOOP AT gt_student INTO gs_student WHERE score >= 60 GROUP BY

"COND关键字实现自定义分组

COND string ( WHEN gs_student \- score >= 60 AND gs_student \- score

< 70 THEN 'GRP1_Pass'

WHEN gs_student \- score >= 70 AND gs_student \- score < 80 THEN

'GRP2_Average'

WHEN gs_student \- score >= 80 AND gs_student \- score < 90 THEN

'GRP3_Good'

ELSE 'GRP4_Excellent' ) DESCENDING

INTO DATA ( custom_grp ).

lt_grp_stu = VALUE gty_t_student ( FOR ls_grp IN GROUP custom_grp (

ls_grp ) ).

"分组处理

LOOP AT GROUP custom_grp ASSIGNING FIELD \- SYMBOL ( <grp> ).

...

ENDLOOP . ENDLOOP .

3

AI解释

ABAP 新语法 GROUPBY - 封面图
ABAP 新语法 GROUPBY - 封面图

仅供参考

ABAP 新语法 GROUPBY - 封面图
ABAP 新语法 GROUPBY - 封面图

这段ABAP代码主要介绍了ABAP中新引入的 ` GROUP BY ` 关键字的用法, ` GROUP BY `

关键字用于内表的分组处理,使得具有相同键值的行被组织在一起,以方便进一步的聚合或处理。

** 关键点: **

• ` itab ` :要进行分组的内表。

• ` result ` :可选项,指定按某一键值的分组结果进行存储。

• ` cond ` :可选项,用于指定进入组循环的条件。

• ` group_key ` :指定用于分组的字段。

• ` ASCENDING|DESCENDING ` :可选项,指定升序或降序排列分组结果。

• ` AS TEXT ` :可选项,将排序结果用文本形式输出。

• ` WITHOUT MEMBERS ` :可选项,指定不输出空组。

• ` group_result ` :可选项,指定存储分组结果的字段。

** 示例解释: **

• 在第一个 ` LOOP ` 中,按照 ` gender ` 字段进行分组,生成 ` grp ` ,然后在内部 ` LOOP AT GROUP ` 中处理每个组。

• 在第二个 ` LOOP ` 中,使用 ` COND ` 关键字自定义分组,将分组结果存储在 ` custom_grp ` 中,然后在内部 ` LOOP AT GROUP ` 中处理每个自定义组。

` GROUP BY ` 关键字使得对内表的分组操作变得简单,可以根据需要轻松地对相同键值的数据进行聚合或其他处理。

ABAP 新语法 GROUPBY - 封面图
ABAP 新语法 GROUPBY - 封面图

以上是今日分享,希望对你有所帮助~~~

欢迎点赞、留言、分享

ABAP 新语法 GROUPBY - 封面图
ABAP 新语法 GROUPBY - 封面图
ABAP 新语法 GROUPBY - 封面图
ABAP 新语法 GROUPBY - 封面图
ABAP 新语法 GROUPBY - 封面图
ABAP 新语法 GROUPBY - 封面图
ABAP 新语法 GROUPBY - 封面图
ABAP 新语法 GROUPBY - 封面图

往期推荐

[ ABAP 新语法-SWITCH、COND

](https://mp.weixin.qq.com/s?__biz=Mzg3OTE2Njg0OQ==&mid=2247495497&idx=1&sn=b28f14fe1ad6bb07ff1894d41a9c41b7&chksm=cf0a3ba7f87db2b1bffdddc196d2a40b0100fb280111fb5754433c56a0415693d0112b350cbb&scene=21#wechat_redirect)

[ ABAP 新语法-LET

](https://mp.weixin.qq.com/s?__biz=Mzg3OTE2Njg0OQ==&mid=2247495544&idx=1&sn=11a10fc7f45113282e9b0e600c49e9e8&chksm=cf0a3b96f87db280c12b7c18df0823a4207c9d52b56b9d773135e12d6173c9636ce091a79cf4&scene=21#wechat_redirect)

[ ABAP 新语法-REDUCE

](https://mp.weixin.qq.com/s?__biz=Mzg3OTE2Njg0OQ==&mid=2247495646&idx=1&sn=370e6863e1c40e7adfd42df62b799993&chksm=cf0a3b30f87db226babd5181e59cc61d9aebb6e759093c1c408fb22f24e5680fca2027db1b59&scene=21#wechat_redirect)

[ ABAP 新语法-FILTER

](https://mp.weixin.qq.com/s?__biz=Mzg3OTE2Njg0OQ==&mid=2247495693&idx=1&sn=505aee012a4e71e200af681d14994f44&chksm=cf0a24e3f87dadf5039dd84f5f62184326d7b59cb803140bf4ce4067bfbb0bc9dd6018d4efb2&scene=21#wechat_redirect)

ABAP 新语法 GROUPBY - 封面图
ABAP 新语法 GROUPBY - 封面图
ABAP 新语法 GROUPBY - 封面图
ABAP 新语法 GROUPBY - 封面图

点个 在看 你最好看

来源说明:本文内容由「ABAP新语法GROUPBY.md」整理生成,仅用于内部技术分享与学习交流。

ABAP Cheat Sheet 补充内容

以下内容整理自 SAP 官方 ABAP 速查表(主题:Internal Tables Grouping),作为本文的扩展补充资料。

Internal Tables: Grouping

Introduction

Similar to SQL's GROUP BY, there is also a GROUP BY for working with internal tables that can be used behind LOOP AT itab or in the form IN GROUP in a table iteration with FOR. It replaces the clumsy group level processing with statements AT NEW ... that relies on the order of table columns and content that is sorted respectively.

Thi cheat sheet explains the grouping of internal tables step by step using a very simple case of an internal table spfli_tab that is filled with data from the database table SPFLI. The following steps show how the content of the internal table can be grouped using LOOP AT GROUP BY.

Grouping by One Column

The simplest form of grouping is by one column without explicitly specifying the output behavior of the group loop:

LOOP AT spfli_tab INTO wa
                  GROUP BY wa-carrid.
       ... wa-carrid ...
ENDLOOP.

Within the loop, there is access to the work area wa, in particular to the component wa-carrid that is used for grouping. The work area wa contains the first line of each group and represents the group in the loop. This is called representative binding.

To access the members of a group, a member loop can be inserted into the group loop:

LOOP AT spfli_tab INTO wa 
                  GROUP BY wa-carrid.
  ...
  LOOP AT GROUP wa INTO DATA(member).
    ... member-... ...
  ENDLOOP.
  
来源说明:本节内容来源:SAP-samples/abap-cheat-sheets (GitHub),原作者 SAP。原 Markdown 文件:11_Internal_Tables_Grouping.md。版权归原作者所有。