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: 

Internal Table process subset of entries

former_member293658
Participant
0 Kudos

Hi ABAP Colleagues

I have an internal table as follows:

ORDER        COST ELEMENT           AMOUNT

921188         601123                         200

921188         601124                         100

921188         601124                         300

921188         601124                         300

921188         601125                         100

921188         601125                         200

921189         601123                         200

921189         601124                         100

921189         601124                         250

921189         601125                         400

Within any order number, if the cost element 601125 occurs more than once, I need to append a record underneath that 601125 record which is a total of the preceeding 601125 records. If within any order number the cost element 601125 only occurs once, then do not append any record. So the above should look like this after processing:

ORDER        COST ELEMENT           AMOUNT

921188         601123                         200

921188         601124                         100

921188         601124                         300

921188         601124                         300

921188         601125                         100

921188         601125                         200

921188         601125                         300  <<-- insert a record which is total of preceeding 2 601125 records

921189         601123                         200

921189         601124                         100

921189         601124                         250

921189         601125                         400  <<-- no need to insert/append a record here as only 1 601125 record exists.

Can you advise please how might I achieve this? We are not on 740 ABAP release.

Thanks and regards

6 REPLIES 6

former_member293658
Participant
0 Kudos

Hi

Also when inserting a summary record under the 601125 records I need to blank out the target amount associated with preceeding 601125 records that have been summarized by the inserted 601125 record. See below.

ORDER        COST ELEMENT           AMOUNT              TARGET

921188         601123                         200                            80

921188         601124                         100                            70

921188         601124                         300                            50

921188         601124                         300                            30

921188         601125                         100                            55

921188         601125                         200                            55

921189         601123                         200                            60

921189         601124                         100                            40

921189         601124                         250                            40

921189         601125                         400                            30

Within any order number, if the cost element 601125 occurs more than once, I need to append a record underneath that 601125 record which is a total of the preceeding 601125 records. If within any order number the cost element 601125 only occurs once, then do not append any record. So the above should look like this after processing:

ORDER        COST ELEMENT    TARGET    AMOUNT                   

921188         601123                         80          200

921188         601124                         70         100

921188         601124                         50         300

921188         601124                         30         300

921188         601125                                       100  <--blank target amount

921188         601125                                       200  <--blank target amount

921188         601125                         55         300  <-- insert record which is total of preceeding 2 * 601125 records

921189         601123                         60         200

921189         601124                         40         100

921189         601124                         40          250

921189         601125                         30          400  <<-- no need to insert/append a record here as only 1 601125 record exists.

Any help on how to achieve this would be appreciated.

Thank you.

0 Kudos

Here is the sample code. I assumed that amount is not zero.

VBELN - Order

KSTAR - Cost element

TARG   - Target

AMUNT - Amount

loop at lt_data into ls_data.

  ls_data2 = ls_data.

  append ls_data2 to lt_data2.

  at end of kstar.

    sum.

    if ls_data-kstar = 601125 and ls_data-amunt <> ls_data2-amunt.

      append ls_data to lt_data2.

    endif.

  endat.

endloop.

lt_data[] = lt_data2[].

loop at lt_data2 assigning <fs_data> where kstar = 601125.

  read table lt_data into ls_data

                     index sy-tabix + 1.

  if sy-subrc = 0.

    if ls_data-vbeln = <fs_data>-vbeln and ls_data-kstar = <fs_data>-kstar.

      ls_data2 = <fs_data>.

      clear <fs_data>-targ.

    else.

      <fs_data>-targ = ls_data2-targ.

    endif.

  endif.

endloop.


Do you want to have this only for some specific Cost element? If not then just delete the code where you see 601125.

-Chandra

0 Kudos

Hi Chandra

Yes I think this will work well, thanks for the idea!

Regards

Michael

0 Kudos

It it solves, please mark the answers as Helpful/Answered and close the discussion.

VenkatRamesh_V
Active Contributor
0 Kudos

Hi Michael,

Sort the internal table by order and cost center, Create a another internal table.

data it_sum like itab.

loop at itab into wa.

collect wa into it_sum.  "Values are summed.

endloop.

loop at itab into wa.

read table it_sum into wa_sum with key order            = wa-order

                                                           cost center   = wa-costcenter.

check sy-subrc is initial.

if wa_sum-amount NE wa-amount.     "Checking the amount

Append wa_Sum to itab.

endif.

endloop.

Hope it helpful,

Regards,

Venkat.

0 Kudos

Hello, can you using at end of  COST ELEMENT field, fist you have to sort it_tab by ORDER, COST ELEMENT. and then at end of COST ELEMENT you can check previewous records if same, than you can do sum else you can skip and continue ( using sy-tabix - 1 )

Hope it can help