Skip to Content
avatar image
Former Member

Select some fields internal table

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

Like this

Header 1 Header 2 Header 3 a 2132 123 a 312 123 a 312 312 b 132 132 c 32 123 c 12 13

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?

Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

2 Answers

  • Best Answer
    avatar image
    Former Member
    Jan 08, 2015 at 06:59 PM

    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.

    Add comment
    10|10000 characters needed characters exceeded

    • Former Member Former Member

      Like I said, I prefer to do it manually. Keep a work are with the last value of the field you are interested in. Compare the next value with the old value. When it changes, do what you need to do and replace the work area with the new value.

      I'm sure there are better ways. This just what I have done since running into the noted problems with AT NEW, ON CHANGE OF, etc.

      Rob

  • avatar image
    Former Member
    Jan 08, 2015 at 06:04 PM

    that table is sorted only by key of the header 1 so maybe your result in a future will be not the correct...

    init variable actual with the first row of the table

    loop.

         save the actual value of header1 in a variable actual.

         when the next value is different append the preview row to result table..

    endloop.

    here a link for abap code https://training.sap.com/shop/certification/c_taw12_731-sap-certified-development-associate---abap-with-sap-netweaver-73…

    regards.

    Alberto

    Add comment
    10|10000 characters needed characters exceeded

    • Former Member

      well, i can try to search the SAP FM and get a table for each one of those aaaa bb cccc. But then again i need to read the last line. i can get that by sy-tabix right?

      and then read the index = sy-tabix.