-3
Former Member

### FIELD SUM IN ABAP

I have to write a code with summarizes a field....So I have a data on report like this

``````AAA BBB CCC 1
AAA BBB CCC 2
SUM 3
-------------
DDD EEE FFF 3
GGG HHH III 4
SUM 7
-------------
TOTAL 10``````
``So i have to summarize fields SUM into one SUM, but the problem is that those are not two variables, it a one variable, and the code loop trough the table and fills the field SUM called G_SUM, so how to make a total sum, ``
10|10000 characters needed characters exceeded
• Please provide the context, and the relevant part of your code.

• Jun 21, 2017 at 05:29 AM

The current answer is use ALV, and better use OO ALV (e.g. CL_SALV_TABLE and look for aggregation subclass) many guides/samples are already available in this forum.

The (very) old way was: read documentation of LOOP AT, AT...ENDAT, and SUM statements...

10|10000 characters needed characters exceeded
• May 16, 2017 at 10:01 AM
-1

Use object ALV.

10|10000 characters needed characters exceeded
• Former Member
Jun 20, 2017 at 04:46 PM
-1

In ALV use both subtotal and total options

10|10000 characters needed characters exceeded
• Jun 21, 2017 at 06:45 AM
-1

Hi,

I assume you are using same variable to calculate subtotal and clearing it after every loop pass.

You can use another variable to store subtotal value before clearing it and increment it after each loop pass.

```Loop at i_tab into w_tab.
clear sub_total.
subtotal = subtotal + w_tab-field1.
grand_total = grand_total + w_tab-field1.
endloop.
```

In that way, grand_total will have total of all sub_totals.

Regards,

Mangesh Parihar

10|10000 characters needed characters exceeded
• Former Member
Jun 21, 2017 at 05:27 AM
-2

Option 1: Insert in your internal table (last line) the total sum of all line items.

Option 2: Use ALV standard Total/Subtotal function.