Skip to Content

Archived discussions are read-only. Learn more about SAP Q&A

Select some fields internal table

Hi, i have an internal table "itab" with a lot of fields. table is sorted.

Like this

Header 1Header 2Header 3
a2132123
a312123
a312312
b132132
c32123
c1213

lets say i want the last lines of the different values of Header 1.

in this case, the last one where header 1 is a, then last one where is b, last one where it is c

a 312 312

b 132 132

c 12 13

is there any simple solution?

Former Member
Former Member replied

hi bruno,

Try this code for the solution.

REPORT  ZR_TEST2.

TYPES: BEGIN OF TY,
         HEADER1 TYPE C LENGTH 10,
         HEADER2 TYPE C LENGTH 10,
         HEADER3 TYPE C LENGTH 10,
        END OF TY.

DATA: WA TYPE TY,
       WA1 TYPE TY,
       ITAB LIKE STANDARD TABLE OF WA,
       ITAB1 LIKE STANDARD TABLE OF WA.

WA-HEADER1 = 'a'.
WA-HEADER2 = '2132'.
WA-HEADER3 = '123'.
APPEND WA TO ITAB.
CLEAR WA.

WA-HEADER1 = 'a'.
WA-HEADER2 = '312'.
WA-HEADER3 = '123'.
APPEND WA TO ITAB.
CLEAR WA.

WA-HEADER1 = 'a'.
WA-HEADER2 = '312'.
WA-HEADER3 = '312'.
APPEND WA TO ITAB.
CLEAR WA.

WA-HEADER1 = 'b'.
WA-HEADER2 = '132'.
WA-HEADER3 = '132'.
APPEND WA TO ITAB.
CLEAR WA.

WA-HEADER1 = 'c'.
WA-HEADER2 = '32'.
WA-HEADER3 = '123'.
APPEND WA TO ITAB.
CLEAR WA.

WA-HEADER1 = 'c'.
WA-HEADER2 = '12'.
WA-HEADER3 = '13'.
APPEND WA TO ITAB.
CLEAR WA.

SORT ITAB BY header1.

LOOP AT ITAB INTO WA.   
   WA1-HEADER1 = WA-HEADER1.
   WA1-HEADER2 = WA-HEADER2.
   WA1-HEADER3 = WA-HEADER3.
  
   AT END OF HEADER1.   
     APPEND WA1 TO ITAB1.
   ENDAT.   
ENDLOOP.

WRITE: / 'FINAL'.

LOOP AT ITAB1 INTO WA.
   WRITE: / WA-header1, WA-header2, wa-header3
ENDLOOP.

0 View this answer in context
Not what you were looking for? View more on this topic or Ask a question