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: 

itab totals

Former Member
0 Kudos

Hi experts,

I have itab(t_det) like this.


AUFNR   MBLNR       MENGE
4006149|0043996297  |1
4006149|0043996338  |1
4006150|0043996294  |1
4006150|0043996332  |1
4006150|0043996292  |1

If aufnr is same i want to add menge.

after addition i want t_det like this.


AUFNR   MBLNR       MENGE
4006149|0043996297  |2
4006150|0043996292  |3

so i have written code like this


  LOOP AT T_DET INTO G_DET.
    G_DET-MENGE = G_DET-MENGE + T_DET-MENGE.
    MODIFY T_DET FROM G_DET TRANSPORTING MENGE.
  ENDLOOP.

is it correct??

thanks

kaki

7 REPLIES 7

Former Member
0 Kudos

Hi kaki,

1. COLLECT

use this abap syntax like this logic.

2. Create new internal table of only two fields.

AUFNR

MENGE

say (stab)

3. Loop at ITAB.

move-correspodnging itab to stab.

collect stab.

endloop.

4. U will get the required SUM in stab.

regards,

amit m.

RichHeilman
Developer Advocate
Developer Advocate
0 Kudos

I would use another internal table in this case.



report  zrich_0001.


data: begin of t_det occurs 0,
      aufnr type aufk-aufnr,
      mblnr type mseg-mblnr,
      menge type mseg-menge,
      end of t_det.

<b>data: begin of t_deta occurs 0,
      aufnr type aufk-aufnr,
      menge type mseg-menge,
      end of t_deta.</b>

<b>loop at t_det.
  clear t_deta.
  move-corresponding t_det to t_deta.
  collect t_deta.
endloop.</b>

Regards,

Rich Heilman

0 Kudos

Hi Rich,

Mblnr is different from each aufnr.

Even though collect works??

kaki

0 Kudos

You should not include MBLNR in your second internal table as it is now meaningless, since you are now aggregating the MENGE field by AUFNR.

Regards,

Rich Heilman

0 Kudos

Hi again,

1. Thats true.

2. This is how COLLECT works.

3. Collect statement

analyses the non-numeric fields

in the internal table (int table in which we colelct)

4. If the COMBINATION of all these non-numeric

fields is FOUND DIFFERENT (from all those already

abvailabel in the internaltable)

then it ADDs a arecord.

              • In your case

the COMBINATION OF FIELDS

is having only ONE FIELD IE. AUFNR.

(the collect table is having

one character field

and another numeric field)

thatgs why !

5. Other wise

it just SUMS up the numeric fields.

regards,

amit m.

0 Kudos

If you look at Rich's code, he is doing a collect on an internal table that has no MBLNR. So it works.

But the question I have is what is the logic for the MBLNR? I thought you will take the first one, but then for the second AUFNR, you took the last one in your example.

Srinivas

Former Member
0 Kudos

hi Kaki,

i think this one works..

data : tot like ..

LOOP AT T_DET INTO G_DET.

tot = g_DET-menge + tot.

ON CHANGE OF G_DET-AUFNR.

G_DET-menge = tot.

modify t_get from g_det transporting menge.

clear tot.

endon.

endloop.

delete adjacent duplicates from T_DET comparing aufnr.

i hope it works..

regards

satesh