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

Former Member
0 Likes
1,490

how to sum up the data in an internal table

10 REPLIES 10
Read only

Former Member
0 Likes
1,382

Loop the itab and sum it.

Within an AT...ENDAT block, you can calculate the contents of the numeric fields of the corresponding control level using the SUM statement.

<b>SUM.</b>

You can only use this statement within a LOOP. If you use SUM in an AT - ENDAT block, the system calculates totals for the numeric fields of all lines in the current line group and writes them to the corresponding fields in the work area (see example in ). If you use the SUM statement outside an AT - ENDAT block (single entry processing), the system calculates totals for the numeric fields of all lines of the internal table in each loop pass and writes them to the corresponding fields of the work area. It therefore only makes sense to use the SUM statement in AT...ENDAT blocks.

<a href="http://help.sap.com/saphelp_47x200/helpdata/en/fc/eb381a358411d1829f0000e829fbfe/frameset.htm">Refer</a>

Rgds,

Jothi.P

**reward if useful.

Read only

Former Member
0 Likes
1,382

hi,

<b>

use <i><u><b>collect</b></u></i> statement for summing numberical fields in internal table.[/b

Regards,

Naresh.

Read only

Former Member
0 Likes
1,382

You can SUM it using COLLECT statement as well

Read only

Former Member
0 Likes
1,382

Hi,

SUM.

Effect

When processing an internal table in a block starting with LOOP and concluded by ENDLOOP, SUM calculates the control totals of all fields of type I, F and P (see also ABAP number types) and places them in the LOOP output area (header line of the internal table or an explicitly specified work area).

You can use the SUM statement both at the end and the beginning of a control group (see also AT FIRST/LAST).

Example

Display the table T with sub-totals:

TYPES: BEGIN OF T_TYPE,

CODE(4),

SALES TYPE P,

DISCOUNT TYPE P,

END OF T_TYPE.

DATA: T TYPE STANDARD TABLE OF T_TYPE WITH NON-UNIQUE

DEFAULT KEY INITIAL SIZE 100,

WA_T TYPE T_TYPE.

...

LOOP AT T INTO WA_T.

AT FIRST.

SUM.

WRITE: /4 'Grand Total:',

20 WA_T-SALES, 40 WA_T-DISCOUNT.

ULINE. SKIP.

ENDAT.

WRITE: / WA_T-CODE,

20 WA_T-SALES, 40 WA_T-DISCOUNT.

AT END OF CODE.

SUM.

WRITE: / WA_T-CODE, 10 'Total:',

20 WA_T-SALES, 40 WA_T-DISCOUNT.

SKIP.

ENDAT.

ENDLOOP.

Regards,

Sruthi

Read only

Former Member
0 Likes
1,382

hai,

loop at itab.

<b>sum</b>. /"this stmt sums the all the numeric fields in itab

endloop.

Read only

amit_khare
Active Contributor
0 Likes
1,382

LOOP AT i_tab1.

write:/ i_tab1-matnr, i_tab1-menge.

AT END OF matnr.

SUM.

write:/ i_tab1-menge.

ENDAT.

ENDLOOP

Regards,

Amit

Read only

Former Member
0 Likes
1,382

hi

have a variable inside the loop and add it with itself and the variable inside the internal table that you want to sum up...if the other fields of the internal table holds the same data, and only numberic fields tend to differ in value, u can use COLLECT to sum up..

if helpful, reward

Sathish. R

Read only

Former Member
0 Likes
1,382

hi,

SUM

Syntax

SUM.

Effect

The statement SUM can only be specified within a loop starting with LOOP, and is only considered within a AT-ENDAT control structure. Prerequisites for using the statement SUM include using the addition INTO in the LOOP statement, and that the specified work area wa is compatible with the row type of the internal table. In addition, SUM cannot be used when the row type of the internal table itab contains components that are tables.

The statement SUM calculates the component total with the numeric data type ( i, p, f) of all rows in the current control level and assigns these to the components of the work area wa. In the control levels FIRST, LAST , and outside of an AT-ENDAT control structure, the system calculates the sum of numeric components of all rows in the internal table.

Example

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.

COLLECT

Syntax

COLLECT wa INTO itab [result].

Effect

This statement inserts the contents of a work area wa either as single row into an internal table itab or adds the values of its numeric components to the corresponding values of existing rows with the same key. As of Release 6.10, you can use result to set a reference to the inserted or changed row in form of a field symbol or data reference.

Prerequisite for the use of this statement is that wa is compatible with the row type of itab. The row type must be flat and all components that are not part of the table key must have a numeric data type ( i, p, f).

If the internal table does not already contain a row with an identical key, the COLLECT statement has the same effect as the following form of the INSERT statement:

INSERT wa INTO TABLE itab [result].

A row, whose position depends on the table key and the table type, is inserted and filled with the contents of wa.

If the internal table already contains one or more rows with an identical key, those values of the components of work area wa that are not part of the key, are added to the corresponding components of the uppermost existing row (in the case of index tables, this is the row with the lowest table index).

The COLLECT statement sets sy-tabix to the table index of the inserted or existing row, in the case of standard tables and sorted tables, and to the value 0 in the case of hashed tables.

Note

Outside of classes, you can omit wa INTO if the internal table has an identically-named header line itab. The statement then implicitly uses the header line as the work area.

Example

Compressed insertion of data from the database table sflight into the internal table seats_tab. The rows in which the key components carrid and connid are identical are compressed by adding the number of occupied seats to the numeric component seatsocc.

DATA: BEGIN OF seats,

carrid TYPE sflight-carrid,

connid TYPE sflight-connid,

seatsocc TYPE sflight-seatsocc,

END OF seats.

DATA seats_tab LIKE HASHED TABLE OF seats

WITH UNIQUE KEY carrid connid.

SELECT carrid connid seatsocc

FROM sflight

INTO seats.

COLLECT seats INTO seats_tab.

ENDSELECT.

Message was edited by:

sunil kumar

Read only

Former Member
0 Likes
1,382

Hi

you can calculate the contents of the numeric fields of the corresponding control level using the SUM statement.

write : itab-(Field_name)

under this u have to write this

Syntax : SUM.

Thanks

Sasmita

Read only

sreeramkumar_madisetty
Active Contributor
0 Likes
1,382

Hi

You can sum it using COLLECT statement if you are performing the operation on Numerical fields.

You SUM for the same purpose if you are required the summation in control level manner.

Regards,

kumar