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: 

Need to SUM on a field with FOR ALL ENTRIES

Former Member
0 Kudos

Hi All,

I need to use something like this appearing below ....but SUM is not allowed with FOR ALL ENTRIES ......Whats the efficient ALTERNATIVE ???

SELECT sum( menge ) INTO TABLE imseg

FROM mseg

FOR ALL ENTRIES IN iresb

WHERE matnr = iresb-matnr

AND bwart = '281'

AND aufpl = iresb-aufpl

AND aplzl = iresb-aplzl.

Any help will be rewarded & appreciated ...

Regards

Jaman

1 ACCEPTED SOLUTION

Former Member
0 Kudos

Hi,

First Fetch the records into a internal table and then Loop into it and sum inside the loop into a variable.

Regards

Karthik D

5 REPLIES 5

Former Member
0 Kudos

Hi,

First Fetch the records into a internal table and then Loop into it and sum inside the loop into a variable.

Regards

Karthik D

0 Kudos

Hi Karthik,

Thanx a ton....

How about the below approach .....its allowed & working fine ....also simplifying my work..

SELECT matnr menge rsnum rspos

INTO (ws-matnr, ws-menge, ws-rsnum, ws-rspos)

FROM mseg

FOR ALL ENTRIES IN iresb

WHERE matnr = iresb-matnr

AND bwart = mvtyp1

AND rsnum = iresb-rsnum

AND rspos = iresb-rspos.

imseg2-matnr = ws-matnr.

imseg2-menge = ws-menge.

imseg2-rsnum = ws-rsnum.

imseg2-rspos = ws-rspos.

COLLECT imseg2.

ENDSELECT.

Former Member

......

You can define RANGES :

ranges : r_blnr for mkpf-mblnr.

  loop at lt_blnr.
    r_blnr-sign = 'I'.
    r_blnr-option = 'EQ'.
    r_blnr-low = lt_blnr-mblnr.
    append r_blnr.
  endloop.

  select sum( menge ) into top_menge from mseg
                          where mblnr in r_blnr.

amitbhawsar0433
Explorer

SUM inside the loop will slow your program because when you have thousands off entry then in loop your sum query will hit database thousands time...

So best approach is define select ranges and put your document number on that internal table and pass it on where with (IN) clause in your select sum query and get your result in your sum internal table and in loop-endloop you can use read statement for sum internal table.