abap

How to generate new own list in an internal table?


List in the internal table at the beginning:

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

To get the output of such a list from the internal table:

code | name | sum
 22  | Jon  | 234.3
 22  | Jon  | 34.2
 12  | Bob  | 999.4
 45  | Anna | 0
 11  | Mike | 234.3

Conditions for the formation of a new (outgoing) list:

  1. If the column name with several identical names (for example with four - Jon) has a value (column sum) 34.5 ... and 0, then throw away all 0 and print only non-zero.
  2. If the column name with several identical names (for example two - Anna) has a value (column sum) 0 - then print only one name with the value 0.
  3. The list can not be sorted - the output must have a list with the same order as the input.

Solution

  • I'm considering lt_grp1 will contains beginning internal table records. And i've declared lt_grp2 of same structure as 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.
    

    Hope this helps!

    For those who think i didn't tested it.

    Here is input table -

    Input table

    Output -

    enter image description here