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

Calculating sum column wise

Former Member
0 Likes
2,539

Hi All,

We are having 6 columns in which amounts are stored. Now, I want the total of a all columns to be displayed in the end. So, tell me how to go for that.

Thanks,

Nishant

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
1,553

HI nishu,

1. COLLECT

2. we have to use collect statement in this fashion.

3. declare a new internal table (CTAB)

same as original table (ITAB)

(take only the numeric fields in it,

and primary key if required for grouping purpose)

3. then use

loop at itab.

move-corresponding itab to ctab.

collect ctab.

endloop.

4. Now in ctab, u will have 1 record,

which will be the sum of all the data,

field wise.

regards,

amit m.

8 REPLIES 8
Read only

Former Member
0 Likes
1,553

hi Nishu,

use <b>COLLECT</b> Statement for the same

i.e,

To create a summate entries in an Internal table COLLECT statement is used. The syntax is as follows: -

<b>COLLECT <wa> INTO <itab></b>

The <wa> must compatible with the line type of <itab>. This creates a sum of all numerical fields in the internal table if the system finds a corresponding entry of table key fields between <wa> and the table.

If it fails to find an entry, the statement behaves like a normal INSERT statement. The only pre-requisite to create a summarized internal table is that all the fields that are not part of the table key must be numerical columns.

Check this out for demonstration

http://help.sap.com/saphelp_erp2005/helpdata/en/fc/eb36d5358411d1829f0000e829fbfe/frameset.htm

Regards,

Santosh

Read only

Former Member
0 Likes
1,554

HI nishu,

1. COLLECT

2. we have to use collect statement in this fashion.

3. declare a new internal table (CTAB)

same as original table (ITAB)

(take only the numeric fields in it,

and primary key if required for grouping purpose)

3. then use

loop at itab.

move-corresponding itab to ctab.

collect ctab.

endloop.

4. Now in ctab, u will have 1 record,

which will be the sum of all the data,

field wise.

regards,

amit m.

Read only

Former Member
0 Likes
1,553

hi,

if u r using an alv reeport use do_sum = 'X'.

IN THE FIELD CATALOG FOR THE CORRESPONDING FIELDS FOR WHICH U WANT TO DO SUM.

BYE

SASI

Read only

Former Member
0 Likes
1,553

Hai Nishu

Collect also appends a record in the senario of modify database table.

if not numeric fields are same then numeric fields will be added by using COLLECT Command

itab-name1 = 'AB'.

itab-name2 = 'CD'.

itab-sal = 20.

collect itab.

itab-name1 = 'AB'.

itab-name2 = 'CD'.

itab-sal = 25.

collect itab.

itab-name1 = 'AB'.

itab-name2 = 'CD'.

itab-sal = 30.

collect itab.

loop at itab.

write : / itab.

endloop.

Thanks & Regards

Sreenivasulu P

Read only

naimesh_patel
Active Contributor
0 Likes
1,553

Hello,

Loop at itab.

at last.

<u><b>sum.</b></u>

endat.

write : itab.

endloop.

Regards,

Naimesh

Read only

Former Member
0 Likes
1,553

You want to display the sum of the column at the end of the report?? like in ..

itab

f1 f2 f3

A 10 20

B 20 10

C 30 40

_________

Sum 60 70

__________

To achieve this, u can do:

loop at itab.

at last.

sum.

write:/ f2, f3.

endat.

endloop.

Or u want to display the sum of the fields in a particular row?? like in..

itab

f1 f2 f3 | sum

A 10 20 | 30

B 20 10 | 30

C 30 40 | 70

loop at itab.

clear itab-sum.

itab-sum = f2 + f3.

modify itab.

endloop.

Read only

hymavathi_oruganti
Active Contributor
0 Likes
1,553

r u using ALV or normal report?

1. if it is ALV, then in the fieldcat for each field,

add do_SUM = 'X'.

2. if it is normal report,

then

ex: loop at itab.

at end of (field).

sum.

endat.

endloop.

<b>sum</b> statement sums the numeric fields

Message was edited by: Hymavathi Oruganti

Read only

Former Member
0 Likes
1,553

Data : tab1 type table of Table_VALUE,

WA_SUM TYPE TABLE_VALUE,

WA_TEMP TYPE TABLE_VALUE.

Clear : wa_sum,

wa_temp.

Loop at tab1 into wa_temp.

wa_sum-field1 = wa_sum-field1 + wa_temp-field1.

wa_sum-field2 = wa_sum-field2 + wa_temp-field2.

wa_sum-field3 = wa_sum-field3 + wa_temp-field3.

wa_sum-field4 = wa_sum-field4 + wa_temp-field4.

wa_sum-field5 = wa_sum-field5 + wa_temp-field5.

wa_sum-field6 = wa_sum-field6 + wa_temp-field6.

-


Write WA_temp for indival values----


i.e write 😕 wa_temp-field1 , ...........

endloop.

-


Sum at the Last of entries----


Write 😕 wa_sum-field1 ,...............

Even the system Statement works the same fassion

that is AT LAST.

Regards

Manoj