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 statement in reports

Former Member
0 Likes
1,711

hi

suppose if sum stmt is encounter it adds the all numeric fields in reports but i wnt only one field is summed how to get this one.

7 REPLIES 7
Read only

Former Member
0 Likes
1,364

Hi Kiran,

When you use SUM i think you need to specify a field or if its used in the internal table control statements then it only sums the target field or the field on which AT events is triggered.

Cheers

VJ

Read only

0 Likes
1,364

hi could u explain briefly ?

Read only

Former Member
0 Likes
1,364

data: begin of itab occurs 0.

field1 type i,

field2 type i,

field3 type i,

end of itab.

data: sum type i.

sum = 0.

loop at itab.

sum = sum + itab-field1.

endloop.

write : sum.

Read only

Former Member
0 Likes
1,364

Hi Kiran,

Are u talking abt sum in select statement..

There u need to specify the fieldname..

example:

SELECT SINGLE SUM( seatsocc )

FROM sflight INTO sum

WHERE fldate LIKE '2002%'.

In case of control statement,

only the field on which event is triggered are summed.

Refer to this example,

Here only T2 is summed..

REPORT DEMO.

DATA: T1(4), T2 TYPE I.

FIELD-GROUPS: HEADER, TEST.

INSERT T2 T1 INTO HEADER.

T1 ='AABB'. T2 = 1. EXTRACT TEST.

T1 ='BBCC'. T2 = 2. EXTRACT TEST.

T1 ='AAAA'. T2 = 2. EXTRACT TEST.

T1 ='AABB'. T2 = 1. EXTRACT TEST.

T1 ='BBBB'. T2 = 2. EXTRACT TEST.

T1 ='BBCC'. T2 = 2. EXTRACT TEST.

T1 ='AAAA'. T2 = 1. EXTRACT TEST.

T1 ='BBBB'. T2 = 1. EXTRACT TEST.

T1 ='AAAA'. T2 = 3. EXTRACT TEST.

T1 ='AABB'. T2 = 1. EXTRACT TEST.

SORT BY T1 T2.

LOOP.

WRITE: /20 T1, T2.

AT END OF T2.

ULINE.

WRITE: 'Sum:', 20 SUM(T2).

ULINE.

ENDAT.

AT END OF T1.

WRITE: 'Different values:', (6) CNT(T1).

ULINE.

ENDAT.

AT LAST.

ULINE.

WRITE: 'Sum:', 20 SUM(T2),

/ 'Different values:', (6) CNT(T1).

ENDAT.

ENDLOOP.

Regards,

Tanveer.

Please mark helpful answers

Message was edited by: Tanveer Shaikh

Read only

Former Member
0 Likes
1,364

Hey Kiran,

Sum statement in report is basically used in Control Break Processing.

After you fill an internal table with data, you often need to write the data out. This output will frequently contain summary information(uch as totals) at the top or bottom of the report.There might also be interim situations (such as subtotals ) within the body of report.

To do this , you can read the data into an internal table and then, within loop at , use the following statements:

1. at first / endat

2.at last / endat

3.at new / endat

4.at end of / endat

5.sum

6.on change of / endon.

The first statement of each of these pairs - except for Sum - controls when the code lies between them is executed.This type of control is called a control break.Their purpose is to ececute the code between them whenever a specific condition in the data is detected during the processing of the loop.

<u><b>Using the Sum statement :</b></u>

syntax :

<b>at first/last/new/end of.

....

sum

....

endat.</b>

where :

<b>...</b> represents any number of lines of code.

Sum calculates a total for the current value of the control level that contains it.

It finds all rows that have the same values within the control level field and all fields of the left of it.

<b>It sums each numeric column to the right of the control level.</b>

It places the totals in the corresponding fields of the work area.

<b>Now can you explain your situation more specifically, then it would be better for me to code the logic accordingly.</b>

Regards,

Kunal.

Read only

0 Likes
1,364

hi whenever sum stmt is there it sums all the numeric fields in itab but iwant sum a particular field in control events

Read only

Former Member
0 Likes
1,364

Hai Kiran

Read this Document

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/4 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:

DATA: BEGIN OF T OCCURS 100,

CODE(4),

SALES TYPE P,

DISCOUNT TYPE P,

END OF T.

...

LOOP AT T.

AT FIRST.

SUM.

WRITE: /4 'Grand Total:',

20 T-SALES, 40 T-DISCOUNT.

ULINE. SKIP.

ENDAT.

WRITE: / T-CODE,

20 T-SALES, 40 T-DISCOUNT.

AT END OF CODE.

SUM.

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

20 T-SALES, 40 T-DISCOUNT.

SKIP.

ENDAT.

ENDLOOP.

Notes

When you use SUM in a LOOP with an explicitly specified output area, this output area must be compatible with the line type of the internal table.

When using LOOP to process a sorted extract (see SORT ), the control total of f at the end of the group appears in the field SUM(f) - - if f is type I , F or P .

Note

Runtime errors

SUM_OVERFLOW : Value too large when calculatng totals in internal table, field too small.

SUM_NO_INTERNAL_TABLE : The SUM statement was used outside a LOOP on an internal table.

SUM_ON_FOREIGN_INTERNAL_TABLE : The SUM statement was used in a LOOP belonging to another ABAP/4 program.

Thanks & Regards

Sreenivasulu P