本文介绍了通过分组在ABAP内部表中查找重复项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我们都知道这些优秀的 ABAP 语句,它允许在单行中查找唯一值:
We all know these excellent ABAP statements which allows finding unique values in one-liner:
it_unique = VALUE #( FOR GROUPS value OF <line> IN it_itab
GROUP BY <line>-field WITHOUT MEMBERS ( value ) ).
但是如何提取重复项呢?是否可以将 GROUP BY
语法用于该任务,或者表格推导式在这里更有用?
But what about extracting duplicates? Can one utilize GROUP BY
syntax for that task or, maybe, table comprehensions are more useful here?
我发现的唯一(虽然不是很优雅)的方法是:
The only (though not very elegant) way I found is:
LOOP AT lt_marc ASSIGNING FIELD-SYMBOL(<fs_marc>) GROUP BY ( matnr = <fs_marc>-matnr
werks = <fs_marc>-werks )
ASSIGNING FIELD-SYMBOL(<group>).
members = VALUE #( FOR m IN GROUP <group> ( m ) ).
IF lines( members ) > 1.
"throw error
ENDIF.
ENDLOOP.
有没有更漂亮的任意键查找重复项的方法?
Is there more beautiful way of finding duplicates by arbitrary key?
推荐答案
所以,我只是把它作为答案,因为我们和 Florian 无法想出更好的办法.
如果有人能够改进它,那就去做吧.
So, I just put it as answer, as we with Florian weren't able to think out something better.
If somebody is able to improve it, just do it.
TYPES tt_materials TYPE STANDARD TABLE OF marc WITH DEFAULT KEY.
DATA duplicates TYPE tt_materials.
LOOP AT materials INTO DATA(material)
GROUP BY ( id = material-matnr
status = material-pstat
size = GROUP SIZE )
ASCENDING REFERENCE INTO DATA(group_ref).
CHECK group_ref->*-size > 1.
duplicates = VALUE tt_materials( BASE duplicates FOR <status> IN GROUP group_ref ( <status> ) ).
ENDLOOP.
这篇关于通过分组在ABAP内部表中查找重复项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!