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: 

How to find the max value among line items in an internal table

Former Member
0 Kudos

hi,

   I have an internal table with so many documents and each document having so many line items, now i want to find the highest amount among the line items for each document and i want to change the highest value containing line item.

                                  eg : my INT Table is like this

                                             doc A

                                             line item 1.

                                            line item 2.

                                            doc B

                                            line item 1.

                                            line item 2.

                                            line item 3.

                                            doc C

                                             line item 1.

                                             line item 2.

                                             line item 3.

                                             line item 4.

Document wise i want to find out the highest value containing line item& i want to deduct some amount from that.

Kindly sujjest me ......

5 REPLIES 5

harshsisodia31
Participant
0 Kudos

Hi

Sort INT by <DOC> <Line Item>.

DELETE ADJACENT DUPLICATE FROM INT COMPARING DOC.

Hope it helps.

former_member184569
Active Contributor
0 Kudos

HI Lokesh.

If you want these values in another table for processing later, we will create an internal table for that, or we will implement the processing (of deducting the value) in that loop itself.

begin of ty_final,        

   bukrs type bukrs,

   belnr type belnr,   

   gjahr type gjahr,    

   buzei type buzei   

  dmbtr type dmbtr,  

end of ty_final.         

data it_Final type table of ty_final.  "Required only if you want to collect all the details to a table.

data :  wa_final type ty_final.

sort itab by bukrs belnr gjahr buzei.

clear wa_final.

loop at itab into wa_tab.

if wa_tab-dmbtr GT wa_final-dmbtr.

    move corresponding wa_tab to wa_final 

endif.

  at end of belnr.

        append wa_final to it_final.   "Required only if you want to collect all the details to a table.

* If you can do the processing here itself, no need to append to the internal table

* Just do the deductions / calculations with the values in wa_final.

        clear wa_final.

   endat.

endloop.

Another option would be just to sort and do the calculations with the first line. for each belnr.

sort itab by bukrs belnr gjahr dmbtr descending.

loop at itab into wa_itab.

  at new belnr.

*wa_itab will have the line item with maximum amount. Do your processing as required. Either append to another table or perform calculations with the values.

   endat.

endloop.

Former Member
0 Kudos

Hi Lokesh

sort int by doc amount descending.

Loop at int into wa.

at new doc.

**Do operations ...

endat.

endloop.

Former Member
0 Kudos

Hi Lokesh,

Simply do this way!

Try this example!

TYPES: BEGIN OF ty_ekpo,
        ebeln TYPE ebeln,
        ebelp TYPE ebelp,
        netwr TYPE ekpo-netwr,
        END OF ty_ekpo.


DATA: it_ekpo TYPE STANDARD TABLE OF ty_ekpo,
       it_ekpo_max TYPE STANDARD TABLE OF ty_ekpo..


    SELECT ebeln ebelp netwr FROM ekpo
           INTO TABLE it_ekpo
           UP TO 10 ROWS.

     SORT it_ekpo[] by ebeln netwr DESCENDING.
     it_ekpo_max[] = it_ekpo[].

     delete ADJACENT DUPLICATES FROM  it_ekpo_max[] COMPARING ebeln.

* so it_ekpo[] - having the original records with all items
* it_ekpo_max[] - will have only items having the maximum price values
* DO THE calculation by looping it_ekpo and reading with it_ekpo_max



Florian
Active Contributor
0 Kudos

Hi lokesh,

all the people just triing to say:

Sort your table in a wise, you got your entry in the first or in the last line and use the READ TABLE statement.

If you need more informations please read trough the official SAP-Helpsite:

Sorting Internal Tables (SAP Library - ABAP Programming (BC-ABA))