在内部表的开头列出:
code | name | sum
22 | Jon | 234.3
22 | Jon | 34.2
22 | Jon | 0
22 | Jon | 0
12 | Bob | 999.4
12 | Bob | 0
45 | Anna | 0
45 | Anna | 0
11 | Mike | 0
11 | Mike | 234.3
要从内部表中获取此类列表的输出:
code | name | sum
22 | Jon | 234.3
22 | Jon | 34.2
12 | Bob | 999.4
45 | Anna | 0
11 | Mike | 234.3
形成新的(传出)列表的条件:
答案 0 :(得分:1)
我正在考虑lt_grp1
将包含开始的内部表记录。而且我已经宣布lt_grp2
与lt_grp1
的结构相同。
DATA: lv_index TYPE i VALUE 0.
APPEND LINES OF lt_grp1 TO lt_grp2.
DELETE ADJACENT DUPLICATES FROM lt_grp2 COMPARING code name.
LOOP AT lt_grp2 INTO ls_grp1.
LOOP AT lt_grp1 INTO ls_grp2
WHERE code = ls_grp1-code
AND name = ls_grp1-name.
lv_index = lv_index + 1.
IF ls_grp2-sum = 0.
IF lv_index > 1.
DELETE lt_grp1 INDEX sy-tabix.
ENDIF.
ELSE.
IF lv_index > 1.
DELETE lt_grp1 WHERE sum = 0
AND code = ls_grp1-code.
ENDIF.
ENDIF.
CLEAR: ls_grp2.
ENDLOOP.
CLEAR : lv_index.
ENDLOOP.
CLEAR :ls_grp1.
LOOP AT lt_grp1 INTO ls_grp1.
WRITE: / ls_grp1-code, ls_grp1-name, ls_grp1-sum.
ENDLOOP.
希望这会有所帮助!
对于那些认为我没有测试过的人。
这是输入表-
输出-
答案 1 :(得分:0)
这具有O(n log(n))个运行时。
lt_copy = lt_original.
SORT lt_copy BY code name ASCENDING sum DESCENDING. "if there is a non-zero line, it is at the top "
DELETE ADJACENT DUPLICATES FROM lt_copy.
LOOP AT lt_original INTO DATA(ls_original).
IF ls_original-sum = 0. "only 0 lines need to be checked"
READ TABLE lt_copy ASSIGNING <fs_max_sum> BINARY SEARCH
WITH KEY code = <fs_original>-code
name = <fs_original>-name.
IF <fs_max_sum>-sum = 0.
"there are only zeros for this code and name, we need it"
ELSE.
DELETE lt_original.
ENDIF.
ENDIF.
ENDLOOP.
答案 2 :(得分:0)
我没有足够的观点来评论其他人的帖子,但是我看到当规范说它不能排序时,他们正在对表格进行排序。不幸的是,您将不得不对该表进行排序(这实际上是幸运的事情)。您需要问为什么它必须以这种特定方式保留。
我尝试了一种不进行排序的方法,但提取数据时却失去了安娜。
LOOP At itab.
READ TABLE itab WITH KEY code = itab-code TRANSPORTING NO FIELDS.
IF SY-SUBRC EQ 0.
IF itab-sum NE 0.
wa-code = itab-code.
wa-name = itab-name.
wa-sum = itab-sum.
APPEND wa.
ENDIF.
ENDIF
ENDLOOP.