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

Summing up

Former Member
0 Likes
735

I have an itab with vbeln matnr netwr edatu fields. I am supposed to display the output containing only matnr and netwr values.

These vbeln may contain same or different matnr. So first I am sorting by matnr. When matnr are same, I am supposed to add netwr values.

I tried using 'collect' keyword like this:

Loop at itab.

collect itab.

endloop.

It's giving a small problem.For ex, when there is a single entry in tab, it adds the netwr values from the header to that entry again thus resulting in summing up single record.

Can you please suggest a correction/ new code here?

Thanks.

1 ACCEPTED SOLUTION
Read only

RichHeilman
Developer Advocate
Developer Advocate
0 Likes
604

When collecting you need to COLLECT into another internal table. You want to collect by MATNR, so that needs to be one field(in this case, its the only field), and NETWR is the field that we are collecting. So you can do this.



report zrich_0001.

data: begin of itab occurs 0,
      vbeln type vbak-vbeln,
      matnr type vbap-matnr,
      netwr type vbap-netwr,
      edatu type vbep-edatu,
      end of itab.

<b>data: begin of itab2 occurs 0,
      matnr type vbap-matnr,
      netwr type vbap-netwr,
      end of itab2.</b>

loop at itab.
<b>  move-corresponding itab to itab2.
  collect itab2.</b>
endloop.


Regards,

Rich Heilman

5 REPLIES 5
Read only

RichHeilman
Developer Advocate
Developer Advocate
0 Likes
605

When collecting you need to COLLECT into another internal table. You want to collect by MATNR, so that needs to be one field(in this case, its the only field), and NETWR is the field that we are collecting. So you can do this.



report zrich_0001.

data: begin of itab occurs 0,
      vbeln type vbak-vbeln,
      matnr type vbap-matnr,
      netwr type vbap-netwr,
      edatu type vbep-edatu,
      end of itab.

<b>data: begin of itab2 occurs 0,
      matnr type vbap-matnr,
      netwr type vbap-netwr,
      end of itab2.</b>

loop at itab.
<b>  move-corresponding itab to itab2.
  collect itab2.</b>
endloop.


Regards,

Rich Heilman

Read only

Former Member
0 Likes
604

hi nuren,

try this..

Loop at itab.

collect itab.

append itab.

clear itab.

endloop.

Read only

Former Member
0 Likes
604

hi Nuren,

Use Refresh keyword or clear statement before you sort or after collecting the values to an internal table...

Regards,

Santosh

Read only

Former Member
0 Likes
604

HI Nuren,

After you use collect.

Clear the internal table.

Regards,

Laxmi.

Read only

Former Member
0 Likes
604

Hi Nuren,

Try with the code.

DATA V_NETWR TYPE VBAP-NETWR.

DATA: BEGIN OF ITAB2 OCCURS 0,

MATNR TYPE VBAP-MATNR,

NETWR TYPE VBAP-NETWR,

END OF ITAB2.

SORT ITAB BY MATNR.

LOOP AT ITAB INTO WA.

AT NEW MATNR.

CLEAR V_NETWR.

READ TABLE ITAB INDEX SY-TABIX.

V_NETWR = V_NETWR + WA-NETWR.

CONTINUE.

ENDAT.

V_NETWR = V_NETWR + WA-NETWR.

AT END OF MATNR.

READ TABLE ITAB INDEX SY-TABIX.

ITAB2-MATNR = WA-MATNR.

ITAB2-NETWR = V_NETWR.

APPEND ITAB2.

CLEAR ITAB2.

ENDAT.

ENDLOOP.

Thanks,

Vinay