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

Former Member
0 Likes
1,082

Hi all,

I have an internal table consisting of three fields. I want to populate the internal table using a select statment for where I sum all records returned by material. Here is my query without the select:

SELECT PRINS PRLAB PREIN

INTO TABLE GT_MSPR_FIELDS

FROM MSPR

FOR ALL ENTRIES IN GT_SUPPLY_DEMAND

WHERE MATNR EQ GT_SUPPLY_DEMAND-MATNR

AND WERKS EQ GT_SUPPLY_DEMAND-WERKS

AND PSPNR IN S_PSPNR.

I tried adding a SUM statment to each field but the select poops. Is there a way to do this?

regards,

Mat

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
967

In general you're better off letting the database do the summing since it is more efficient. Can you show us the SQL that gives you an error? Do you have a <b>GROUP BY</b> statement in the code?

7 REPLIES 7
Read only

Former Member
0 Likes
967

Hi, take a look :

The "collect" statement will do the sum.

data : GT_MSPR_FIELDS2 like GT_MSPR_FIELDS with header line.


loop at GT_MSPR_FIELDS2.
move-corresponding GT_MSPR_FIELDS to GT_MSPR_FIELDS2
collect GT_MSPR_FIELDS2.
endloop.

Best regards,

Erwan

Read only

Former Member
0 Likes
967

Hi,

Loop at the internal table.

use control break processing statments

write SUM

end the loop.

Regards,

Senthil

Read only

Former Member
0 Likes
968

In general you're better off letting the database do the summing since it is more efficient. Can you show us the SQL that gives you an error? Do you have a <b>GROUP BY</b> statement in the code?

Read only

0 Likes
967

Hi Bjorn,

I have an itab that includes matnr prins prlab prein

SELECT MATNR sum(PRINS) sum(PRLAB) sum(PREIN)

INTO TABLE GT_MSPR_FIELDS

FROM MSPR

FOR ALL ENTRIES IN GT_SUPPLY_DEMAND

WHERE MATNR EQ GT_SUPPLY_DEMAND-MATNR

AND WERKS EQ GT_SUPPLY_DEMAND-WERKS

AND PSPNR IN S_PSPNR.

that is what I had in mind....thanks much.

Read only

Former Member
0 Likes
967

select matnr werks sum(prins) sum(prlab) sum(prein)

into table gt_mspr_fields

from mspr

for all entries in gt_supply_demand

where

MATNR EQ GT_SUPPLY_DEMAND-MATNR

AND WERKS EQ GT_SUPPLY_DEMAND-WERKS

AND PSPNR IN S_PSPNR

GROUP BY matnr werks.

Read only

0 Likes
967

doesn't appear to like the for all entries in componenet of the select....

Read only

0 Likes
967

In that case can you loop through the gt_supply internal table and use the particular select statement.

(Surely without for all entries clause)