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

SUM in internal table

Former Member
0 Likes
1,157

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
Read only

naimesh_patel
Active Contributor
0 Likes
810

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

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

4 REPLIES 4
Read only

naimesh_patel
Active Contributor
0 Likes
811

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

Read only

former_member187255
Active Contributor
0 Likes
810

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.

Read only

Former Member
0 Likes
810

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.

Read only

Former Member
0 Likes
810

ok