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

Math operation on a table

Former Member
0 Likes
1,932

I have this column named "Price" and I need to add up all the entries to that column. I know I can run through each row and calculate the sum but is there any function or method already available in ABAP development that I can use? Can you pls show me other math operations available already? Any list? im a newbie. <b>big thanks.</b>

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
1,229

you can use control break statements for the same

loop at itab.
 AT LAST. 
    SUM.
  write : itab-price.  <-------writes the sum of price field
 ENdAT.
endloop.

you can use control break statements for the same

loop at itab.
 AT LAST. 
    SUM.
  write : itab-price.  <-------writes the sum of price field
 ENdAT.
endloop.

6 REPLIES 6
Read only

Former Member
0 Likes
1,230

you can use control break statements for the same

loop at itab.
 AT LAST. 
    SUM.
  write : itab-price.  <-------writes the sum of price field
 ENdAT.
endloop.

Read only

0 Likes
1,229

<b>thanks for ALL the reply</b>. 😃

Read only

Former Member
0 Likes
1,229

Hi,

Go through the attached doc for all math functions in SAP.

Mathematical Functions

The argument arg of a mathematical function must represent a numeric value. The data type of the return value is determined either by the argument of the function (overloaded functions) or by the function itself (floating point functions).

Outside of an arithmetic expression, the argument of a mathematical function must be a single, numeric data object. Within an arithmetic expression - that is, in the statement COMPUTE - the argument of a mathematical function can also be an arithmetic expression. In particular, a mathematical function in an arithmetic expression can have a different built-in function or a functional method as argument.

Overloaded Functions

The following table shows the overloaded functions where the data type of the argument determines the data type of the return value.

Function func Return value

abs Absolute value of the argument arg

sign Plus/minus sign of the argument arg: -1, if the value of arg is negative; 0 if the value of arg is 0; 1 if the value of arg is positive.

ceil Smallest integer number that is not smaller than the value of the argument arg.

floor Largest integer number that is not larger than the value of the argument arg.

trunc Value of the integer part of the argument arg

frac Value of the decimal places of the argument arg

If the argument of an overloaded function does not have any numeric data type i, p, or f, its data type determines the type of return value as follows:

d and t give i

c, n and string give p

x and xstring give i

Before the function is calculated, the argument is converted into the respective type:

Floating Point Functions

The following table shows the floating point functions where the return value is always of the type f. Floating point functions expect an argument of the type f. Data objects of other data types are converted, before calculation of the function, into the data type f.

Funktion func Meaning Definition Area

acos Arcuscosinus [-1,1]

asin Arcussinus [-1,1]

atan Arcustangens

cos Cosinus

sin Sinus

tan Tangens

cosh Hyperbelcosinus

sinh Hyperbelsinus

tanh Hyperbeltangens

exp Exponential function for basis e [-709, 710]

log Natural logarithm > 0

log10 Logarithm for basis 10 > 0

sqrt Square root >= 0

For the functions for which a definition area is specified, the value of arg must be in the specified limits. For the exponential function exp an error-free execution is guaranteed for the arguments within the definition ares, because the results then lie within the value range for floating point numbers according to IEEE-754. For arguments smaller than -709, the result is always 0, depending on the platform, or as of a certain value a treatable exception of class CX_SY_ARITHMETIC_OVERFLOW is triggered. For arguments greater than +718, it also leads to this exception. The trigonometry functions sin, cos, and tan are defined for arbitrary arguments, but the results are inexact whenever the amount of the argument is greater than approximately 100.000.000.

Note

The function atan may be undefined for odd-numbered multiples of pi/2, but the definition area for atan is not limited, because an argument of the functions can never contain exactly the value of pi/2.

Exceptions that can be handled

The exceptions that can be handled and can occur during calculation of a mathematical function are subclasses of the classes CX_SY_ARITHMETIC_ERROR und CX_SY_CONVERSION_ERROR.

reward if useful

Regards,

Anji

Read only

Former Member
0 Likes
1,229

Hi...

There is a statement <b>SUM</b>.

Go through the following link:

http://help.sap.com/saphelp_erp2005vp/helpdata/en/9f/db9f1f35c111d1829f0000e829fbfe/frameset.htm

see the following Ex:

REPORT ZTEST.

DATA:

BEGIN OF ITAB OCCURS 0,

NAME(10) TYPE C,

SALARY TYPE I,

END OF ITAB.

ITAB-NAME = 'Suresh'.

ITAB-SALARY = 1000.

APPEND ITAB.

ITAB-NAME = 'xyz'.

ITAB-SALARY = 2000.

APPEND ITAB.

ITAB-NAME = 'dddd'.

ITAB-SALARY = 3000.

APPEND ITAB.

ITAB-NAME = 'abc'.

ITAB-SALARY = 5000.

APPEND ITAB.

ITAB-NAME = 'zzzz'.

ITAB-SALARY = 10000.

APPEND ITAB.

LOOP AT ITAB.

AT FIRST.

<b> SUM.</b>

WRITE: ' Total salary is :', ITAB-SALARY.

ENDAT.

ENDLOOP.

Reward points if useful......

Suresh......

Read only

Former Member
0 Likes
1,229

I had a similar requirement and i could't find any existing function which could do this.

Read only

Former Member
0 Likes
1,229

In Internal Tables we have Control Structures for Control Level Processing.

The Control Structures we have are

1. ... FIRST

2. ... |{END OF} comp

3. ... LAST

The Math Operation Performed on the internal tables is <b>SUM.</b>

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.

If Helpful ,Reward Points

Regards,

Satish