Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

row wise maximum number

Former Member
0 Kudos

Hi,

Please let me know how can i find row wise maximum number, suppose, in an internal table i have 2 records with 6 columns(6th column = results).

Ex: itab

  col1 | col2 | col3 | col4 | col5  |  Results

> 10      50      70      90      20         90

>  1        8       12      5        2          12

Thanks in advance.

6 REPLIES 6

Former Member
0 Kudos

Hi

You can use the field-symbol in order to scan all columns of a record of your internal table: here you can check the value (column by column): you only need to save the max value detected after each control

Max

0 Kudos

Hi,

Could you please provide a sample piece of code ?

Thanks in advance.

0 Kudos

Ok

it's very easy control, yuo can use many ways to do it, for example if you need to check the value only for certain fields of your internal table you can use a structure having those only those fields.


FIELD-SYMBOLS: <FS_COLUMN> TYPE ANY.

DATA: MAX_VALUE TYPE I,

DATA: BEGIN OF CHECK_VALUE,

               COLUMN1 LIKE ITAB-COLUM1,

               COLUMN2 LIKE ITAB-COLUM2,

               COLUMN3 LIKE ITAB-COLUM3,

               COLUMN4 LIKE ITAB-COLUM4,

               COLUMN5 LIKE ITAB-COLUM5,

               ................................................

           END    OF CHECK_VALUE.

LOOP AT ITAB.

   CLEAR MAX_VALUE.

   MOVE-CORRESPONDING ITAB TO CHECK_VALUE.

   DO.

       ASSIGN COMPONENT SY-INDEX OF STRUCTURE CHECK_VALUE TO  <FS_COLUMN>.

        IF SY-SUBRC <> 0. EXIT. ENDIF.

  

        IF MAX_VALUE < <FS_COLUMN>.

           MAX_VALUE = <FS_COLUMN>.

        ENDIF.

  ENDDO.

  ITAB-COLUMN_MAX = MAX_VALUE.

  MODIFY ITAB.

ENDLOOP.

Max

Former Member
0 Kudos

Lets say currently results field is blank in the ITAB.

Field-Symbols : <fs1> type any,

                          <fs_itab> like line of itab.

loop at itab assigning <fs_itab>.

while sy-subrc eq 0.

assign component sy-index of structure wa_itab to <fs1>.

     if     <fs_itab>-result lt <fs1>

          <fs_itab>-result = <fs1>.

     endif.

endwhile.

endloop.

Hope this helps.

suresh_kutam
Participant
0 Kudos

Hello Mahen,

you will achieve your result with the below code,

FIELD-SYMBOLS: <l_fs_tab>   TYPE STANDARD TABLE,     "For dynamic internal table

               <l_fs_wa>    TYPE any.                "for dynamic work area

*// Build IDOC's for the data selected

        LOOP AT <l_fs_tab> ASSIGNING <l_fs_wa> .

   

          DO.

* Assign every field of this structure subsequently to the untyped fieldsymbol.

            ASSIGN COMPONENT sy-index OF STRUCTURE <l_fs_wa> TO <l_fs_field>.

            IF sy-subrc NE 0.

               <l_fs_wa>-result =   l_v_max          

              EXIT.

            ELSE.

        

            if <l_fs_field> GT l_v_max.

                l_v_max = <l_fs_field>.

               ENDIF.

                ENDIF.

            ENDIF.

          ENDDO.

        ENDLOOP. "<l_fs_tab>

Thanks,

SK

VenkatRamesh_V
Active Contributor
0 Kudos

Hi,

Kindly view the source code.

REPORT YTEST.

TYPES: BEGIN OF ty_tab,

        col1(03)   type N,

        col2(03)   type N,

        col3(03)   type N,

        result(03) type N,

        END OF ty_tab.

DATA: it_tab type table of ty_tab,

       wa_tab like line  of it_tab,

       lv_max(03) type N.

FIELD-SYMBOLS: <fs>  type ty_tab,

                <val> type any.

wa_tab-col1 = 10.

wa_tab-col2 = 5.

wa_tab-col3 = 15.

append wa_tab to it_tab.

clear  wa_tab.

wa_tab-col1 = 10.

wa_tab-col2 = 25.

wa_tab-col3 = 15.

append wa_tab to it_tab.

clear  wa_tab.

wa_tab-col1 = 30.

wa_tab-col2 = 5.

wa_tab-col3 = 15.

append wa_tab to it_tab.

clear  wa_tab.

LOOP AT  it_tab into wa_tab.

CLEAR lv_max.

ASSIGN  wa_tab to  <fs> casting.

Do.

IF sy-subrc <> 0.

EXIT.

ENDIF.

ASSIGN COMPONENT sy-index OF STRUCTURE <fs> to <val>.

IF lv_max < <val>.

lv_max = <val>.

ENDIF.

ENDDO.

wa_tab-result = lv_max.

MODIFY it_tab from wa_tab.

ENDLOOP.

LOOP AT it_tab into wa_tab.

WRITE: wa_tab.

ENDLOOP.

Hope it helpful,

Regards,

Venkat.