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 in SELECT query

Former Member
0 Likes
6,629

data : lv_zgdmeng TYPE i.

SELECT SUM( zgdmeng )

INTO lv_zgdmeng

FROM zgdpw

WHERE matnr EQ ls_prores-matnr

AND charg EQ lv_batch.

and in table zgdmeng is numc

i m getting an error saying sum can be used with numeric fields..

how to do this..

thanks and regards

5 REPLIES 5
Read only

Former Member
0 Likes
5,645

hi Neha,

do this way...select entire data and loop through the data to calculate the sum of it ...


SELECT zgdmeng 
INTO table it_zgdpw
FROM zgdpw
WHERE matnr EQ ls_prores-matnr
AND charg EQ lv_batch.

loop at it_zgdpw.
  lv_zgdmeng = lv_zgdmeng + it_zgdpw-zgdmeng.
endloop.

Regards,

Santosh

Read only

Former Member
0 Likes
5,645

1. select all the zgdmeng in a internal table.

SELECT zgdmeng

INTO table lt_zgdmeng

FROM zgdpw

WHERE matnr EQ ls_prores-matnr

AND charg EQ lv_batch.

2. The loop the table and add all the quantities.

Loop lt_zgdmeng to lw_zgdmeng.

tot_zgdmeng = tot_zgdmeng + lw_zgdmeng.

clear: lw_zgdmeng.

endloop.

Read only

Former Member
0 Likes
5,645

hi

i think SUM clause will work with I , F or P only not with NUMC ,so try it with the change .

reward if helpful.

Read only

Former Member
0 Likes
5,645

Hi Neha,

just define lv_zgdmeng like ztablaename-zgdmeng

your problem will be resolved.

Gaurav

Read only

Former Member
0 Likes
5,645

hi neha,

If you only want to use data for calculations, it is often more efficient to use the aggregate functions of the SELECT clause than to read the individual entries from the database and perform the calculations in the ABAP program.

Aggregate functions allow you to find out the number of values and find the sum, average, minimum, and maximum values. Following an aggregate expression, only its result is transferred from the database.

ex:-

DATA RESULT TYPE P DECIMALS 2.

SELECT <agg>( [DISTINCT] COL_2 )

INTO RESULT

FROM TEST.

WRITE RESULT.

  • The arithmetic operators AVG and SUM only work with numeric fields like f, i ,p not other than this .

*The spaces between the parentheses and the arguments of the aggregate expressions must not be left out.

thanks regard ,

sandeep patel

  • award point if it is helpful.