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: 

SUM in internal table

Former Member
0 Kudos

How to get sum for a particular field in internal table?

I hv used sorted table.

I want group the field for which I want to take sum <b>depending</b> on currency field.

I am not able to use AT- NEW - END - END AT

1 ACCEPTED SOLUTION

naimesh_patel
Active Contributor
0 Kudos

Hello,

Take one more internal table with your required field as a group criteria and amount.

Like:


data: begin of itab occurs 0,
fld1 type fld1,
wears type waers,
amount type dmbtr,
end of itab.

loop at <sorted_tab>.
move-corresponding <sorted_Tab> to itab.
collect itab.
clear  itab.
endloop.

After this ITAB will have the data with SUM on the amount field.

Regards,

Naimesh Patel

4 REPLIES 4

naimesh_patel
Active Contributor
0 Kudos

Hello,

Take one more internal table with your required field as a group criteria and amount.

Like:


data: begin of itab occurs 0,
fld1 type fld1,
wears type waers,
amount type dmbtr,
end of itab.

loop at <sorted_tab>.
move-corresponding <sorted_Tab> to itab.
collect itab.
clear  itab.
endloop.

After this ITAB will have the data with SUM on the amount field.

Regards,

Naimesh Patel

former_member187255
Active Contributor
0 Kudos

Suhas,

If you want to use AT- NEW - END - END AT based on Currency Field then the Currency Field should be the <b>First</b> field in the Internal table.....

Chandra.

Former Member
0 Kudos

hi,

do the following:

declare one more itab to collect the sum, say itab2 and one temporary variable v1 type waers to store currency key.

loop at itab1 into wa1.

if sy-tabix = 1.

v1 = wa1-waers.

move wa1 to wa2.

endif.

else.

if v1 = wa1-waers.

wa2-amount = wa2-amount + wa1-amount.

else.

append wa2 to itab2.

v1 = wa1-waers.

clear wa2.

endif.

endloop.

Former Member
0 Kudos

ok