Find Maximum Value or Is Empty Internal Table 

How to find Maximum Value in Internal Table

There is one internal table containing 3 columns.

I want to find the maximum value of 2nd column in that internal table . If I use loop at tab, that becomes performance issue, when there are more no. of records.
 

sort internaltable by secondcolumn descending.
read table internaltable index 1.
thelargestvalueofsecondcolumn = internaltable-secondcolumn.
 

Though we sort internal table on second column ,it sorts, first ,based on the first column and then it sorts on second column.So we may not get the maximum value on top of second column.
If I am true,please give me some suggestion.

Ravi
 

If the two columns are A and B then

SORT ITAB BY B DESCENDING.
READ TABLE ITAB INDEX 1.
WRITE:/ ITAB-B.

works very fine.

Saint.
 

You have internal table with three rows in it. Ok...now when you add the rows into the internal tables, generally you writes,

APPEND IT..........instead of that you write like this :
APPEND IT sorted by  column name           ................ this column name must be that column for which you have to find the max. value.

now write :

READ TABLE IT index 2.

you will get the second max. value in the internal table.

for example, copy the following code and test it. You'll get the result.

REPORT ZAPPEND.

data: begin of it occurs 3,
          sales type p decimals 2,
          name(10),
end of it.

it-sales = 100.
it-name = 'Ulhas'.

append it sorted by sales.

it-sales = 50.
it-name = 'Ravi'.

append it sorted by sales.

it-sales = 150.
it-name = 'Ram'.

append it sorted by sales.

it-sales = 75.
it-name = 'Sham'.

append it sorted by sales.

it-sales = 200.
it-name = 'Madan'.

append it sorted by sales.

it-sales = 100.
it-name = 'Javed'.

append it sorted by sales.

loop at it.
     write: / it-sales, it-name.
endloop.

read table it index 2.

write : / 'Second maximun row is'.
write: / it-sales, it-name.

Ulhas

How To Know Whether Internal Tables Is Empty

* Check  Whether Table is Empty
IF ITAB[] is initial.
   WRITE: / 'TABLE EMPTY'.
ENDIF.

* Assuming that the data have been uploaded into internal table ITAB. You then can ignore the first line of the internal table
Delete ITAB index 1.          ( before you do your operation such as batch input or others )

* To know the last record insert in a table
DESCRIBE TABLE ITAB LINES SY-TFILL.
READ TABLE ITAB INDEX SY-TFILL.

More Function Module
Functions / SAP Script / ALV

Tables
Database Table

ABAP Books List
ABAP/4 Certification, Programming, Smartforms, Sapscripts and Object Oriented Programming Books

Smart Forms
SAP Smartforms

ABAP Menu:
ABAP Example Hints and Tips

Return to Index:-
SAP ABAP/4 Programming, Basis Administration, Configuration Hints and Tips

(c) www.gotothings.com All material on this site is Copyright.
Every effort is made to ensure the content integrity.  Information used on this site is at your own risk.
All product names are trademarks of their respective companies.  The site www.gotothings.com is in no way affiliated with SAP AG.
Any unauthorised copying or mirroring is prohibited.