Application Development and Automation 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: 
Read only

Problem with internal table

Former Member
0 Likes
897

Hi all abap sxperts.

my problem:

I have an IT filled with some records.

I must have only one record for 'OPBEL' with the sum of one field.

example to explain better:

Opbel

now > 000444 10

000444 14

000022 30

000022 10.

I want > 000444 24

000022 40.

Help me, points for all.

Bye

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
875

I have a lot of numeric fields in the record that no need the sum

10 REPLIES 10
Read only

Former Member
0 Likes
875

hi Brasi,

Use <b>Collect</b> statement after the values get populated into an internal table ..

Regards,

Santosh

Read only

Former Member
0 Likes
875

Define a second internal table like the first. Loop at your internal table and use the collect statement to collect records into the second. The second table will contain unique entries and all numeric fields will be summed.

loop at itab.

collect itab into itab2.

endloop.

Read only

Former Member
0 Likes
876

I have a lot of numeric fields in the record that no need the sum

Read only

0 Likes
875

Define the second table to contain just OPBEL and the field you want summed.

loop at itab.

move-corresponding itab to itab2.

collect itab2.

endloop.

Read only

0 Likes
875

Hi Rhino,

If you want to have this OPBEL back in original itab

after summing up into itab2, do this way after what

Michael has suggested.

loop at itab. "Michael's code

move-corresponding itab to itab2.

collect itab2.

endloop. "Michael's code

<b>Loop at itab.

read table itab2 with key OPBEL = itab-opbel.

if sy-subrc eq 0.

itab-<field> = itab2-<field>.

modify itab index sy-tabix.

endif.

endloop.</b>

This way, you will have all the data back in itab.

Hope this will hep you.

Regards,

Vivek

Read only

Former Member
0 Likes
875

hi,

Try this code.

  types : begin of t_itab,
            fld(6),
            num type i,
            val type i,
          end of t_itab.
  data: itab type standard table of t_itab with header line,
        itab2 type standard table of t_itab with header line,
         lv_sum like itab-num.

  sort itab by fld.
  loop at itab.
    lv_tabix = sy-tabix.
    at end of fld.
       sum.
       lv_sum = itab-num.
       clear itab.
       read table itab index lv_tabix.
       if sy-subrc = 0.
          itab2 = itab.
          itab2-num = lv_sum.
          append itab2.
          clear itab2.
       endif.
    endat.
  endloop.

Regards,

Sailaja.

Read only

Former Member
0 Likes
875

hi,

then you can loop at the internal table and add the field that you want in the other table.

itab1 : f1
        f2
        f3
        f4
itab2 : f1
        f2
loop at itab1.
 v_field = v_field + itab1-f2.
 at end of f1.
  itab2-f1 = itab1-f1.
  itab2-f2 = v_field.
  append itab2.
  clear v_field.
 endat.
endloop.

Regards,

Richa

Message was edited by: Richa Singh

Read only

Former Member
0 Likes
875

As your requirement is to sum only the one field and not the other numeric fields..you can use the below logic...

Define an internal table itab with the structure

OPBEL,

ltot.

While you are collecting information for internal table IT you can initialise the values for itab and use COLLECT. If that is not possible ...you can

LOOP at IT.

itab-opbel = it-opbel.

itab-ltot = it-lnum.

collect itab.

endloop.

Read only

Former Member
0 Likes
875

Hi,

data itab1 like itab occurs 0 with header line.

loop at itab.

itab1 = itab.

collect itab1.

endloop.

Now your itab will have

000444 24

000022 40.

Regards

Amole

Read only

Former Member
0 Likes
875

thank you all, problem solved.