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
3,063

Dear All,

In our Internal Table there are several fields,say F1-F2-F3-F4-F5-F6.On the basis of field F2 we need to Sum up fields F3 and F4, but not F5 and F6.

This all fields F3-F6 are Quan Type.

Thx in Adv.

Dear All,

In our Internal Table there are several fields,say F1-F2-F3-F4-F5-F6.On the basis of field F2 we need to Sum up fields F3 and F4, but not F5 and F6.

This all fields F3-F6 are Quan Type.

Thx in Adv.

4 REPLIES 4
Read only

Former Member
0 Likes
861

HI,

First sort the table:

SOrt itab by F2.

LOOP AT itab.

COLLECT itab into itab1. "Itab1 is an internal table similar to itab structure

ENDLOOP.

Your final results are in itab1 internal table.

Regards

Subramanian

Read only

Former Member
0 Likes
861

DATA: sflight_tab TYPE SORTED TABLE OF sflight

WITH UNIQUE KEY carrid connid fldate,

sflight_wa LIKE LINE OF sflight_tab.

SELECT *

FROM sflight

INTO TABLE sflight_tab.

LOOP AT sflight_tab INTO sflight_wa.

AT NEW connid.

WRITE: / sflight_wa-carrid,

sflight_wa-connid.

ULINE.

ENDAT.

WRITE: / sflight_wa-fldate,

sflight_wa-seatsocc.

AT END OF connid.

SUM.

ULINE.

WRITE: / 'Sum',

sflight_wa-seatsocc UNDER sflight_wa-seatsocc.

SKIP.

ENDAT.

AT END OF carrid.

SUM.

ULINE.

WRITE: / 'Carrier Sum',

sflight_wa-seatsocc UNDER sflight_wa-seatsocc.

NEW-PAGE.

ENDAT.

AT LAST.

SUM.

WRITE: / 'Overall Sum',

sflight_wa-seatsocc UNDER sflight_wa-seatsocc.

ENDAT.

ENDLOOP.

REWADS IF HELPFUL

Read only

Former Member
0 Likes
861

hi

You can use collect statement if you have all the non-numeric fields are similar. Collect will sum up all the numeric fields where the non-numeric fields are same.

if you have other fields which are not similar, in your case the field 1, which you are not considering. I would suggest you to use Sum statement. Sort the table on field 2 and then sum up the values 'AT END OF' Statement.

sample code:

Control level processing for creating a list. At the end of line groups, the total of reserved places is calculated and issued.

DATA: sflight_tab TYPE SORTED TABLE OF sflight

WITH UNIQUE KEY carrid connid fldate,

sflight_wa LIKE LINE OF sflight_tab.

SELECT *

FROM sflight

INTO TABLE sflight_tab.

LOOP AT sflight_tab INTO sflight_wa.

AT NEW connid.

WRITE: / sflight_wa-carrid,

sflight_wa-connid.

ULINE.

ENDAT.

WRITE: / sflight_wa-fldate,

sflight_wa-seatsocc.

AT END OF connid.

SUM.

ULINE.

WRITE: / 'Sum',

sflight_wa-seatsocc UNDER sflight_wa-seatsocc.

SKIP.

ENDAT.

AT END OF carrid.

SUM.

ULINE.

WRITE: / 'Carrier Sum',

sflight_wa-seatsocc UNDER sflight_wa-seatsocc.

NEW-PAGE.

ENDAT.

AT LAST.

SUM.

WRITE: / 'Overall Sum',

sflight_wa-seatsocc UNDER sflight_wa-seatsocc.

ENDAT.

ENDLOOP.

Exceptions

Catchable Exceptions

CX_SY_ARITHMETIC_OVERFLOW

Cause: Overflow when calculating totals

Runtime Error: ADDF_INT_OVERFLOW (catchable)

Cause: Value too large when calculating totals in internal table, field too small

Runtime Error: SUM_OVERFLOW

Non-Catchable Exceptions

Cause: The statement SUM was used outside a LOOP processing of an internal table.

Runtime Error: SUM_NO_INTERNAL_TABLE

Cause: The statement SUM was used within a AB>LOOP processing belonging to another ABAP program.

Runtime Error: SUM_ON_FOREIGN_INTERNAL_TABLE

Cause: The statement SUM was used within a loop starting with LOOP ... ASSIGNING.

Runtime Error: SUM_NO_ASSIGNING

-

Santosh

Read only

Former Member
0 Likes
861

sort itab by F2.

Loop at itab into wa_itab.

F3_sum = F3_sum + wa_itab-F3.

F4_sum = F4_sum + wa_itab-F4.

At last F2.

**Here in this event, you will get the sum of F3s in F3_sum and sum of F4s in F4_sum.

clear F3_sum, F4_sum.

Endat.

Endloop.

I hope it helps.

Best Regards,

Vibha