Application Development 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: 

SELECT SUM( field1 field2 ) INTO C1

Former Member
0 Kudos
60,124

Is that possible with a select statement in ABAP or do I need to sum each row by itself then add them together afterwords?

1 ACCEPTED SOLUTION

andreas_mann3
Active Contributor
6,400

Hi all,

try this :

data sum type bseg-dmbtr.

exec sql.

SELECT sum(btr1 + btr2) into :sum FROM ztab

where bukrs = 1000

endexec.

Grx Andreas

6 REPLIES 6

PrasadRaghavan
Employee
Employee
0 Kudos
6,400

Hi

Not sure what your requirement is.

Assuming your table has field1 and field2 and entries as below.

-


Field1 | Field2 |

-


|

1 | 2 |

3 | 4 |

5 | 6 |

-


Do you want to sum up all rows of field1 ( 135 = 9)

with all rows of field2 ( 246 = 12) and put the result (9+12 = 21) into C1 ?

Thanks

Prasad

0 Kudos
6,400

Exactly, we have a table with several rows, Two of these rows are type i.

We would like to sum of the value of both rows in the select statement.

Right now we could only get it to work by


DATA: C1 TYPE i,
      C2 TYPE i,
      total type i.

SELECT SUM( field1 ) SUM( fiedl2 ) into ( c1 c2 )from tablename.

total = c1 + c2.

andreas_mann3
Active Contributor
6,401

Hi all,

try this :

data sum type bseg-dmbtr.

exec sql.

SELECT sum(btr1 + btr2) into :sum FROM ztab

where bukrs = 1000

endexec.

Grx Andreas

0 Kudos
6,400

What type is bseg-dmbtr ?? I don't find it in my system?

0 Kudos
6,400

Hi Craig,

"Namen sind Schall und Rauch "

data sum type p decimals 2.

regards

Former Member
0 Kudos
6,400

Hi Craig,

this is not possible with a select statement in ABAP. You can use the routine mentioned by you:

DATA: C1 TYPE i,

C2 TYPE i,

total type i.

SELECT SUM( field1 ) SUM( fiedl2 ) into ( c1 c2 )from tablename.

total = c1 + c2.

Of course you can use the exec sql statement proposed by Andreas, but you have to select for the cllient explicitly. There are some disadvantages for native sql, too:

1. platform dependent

2. no syntax check for the native sql part

3. you bypass the SAP database buffer

For more details refer to the online help about EXEC SQL.

Cheers,

Morten