‎2008 Jan 10 4:00 PM
What is wrong with my coding?
DATA:
BEGIN OF itab1 OCCURS 0,
matnr type mseg-matnr,
mjahr type mseg-mjahr,
erfmg type mseg-erfmg,
END OF itab1.
select MATNR MJAHR ERFMG
into corresponding fields of table itab1
from mseg
where bwart = '501'.
collect itab1.
sort itab1.
I thought I get a sum per Jahr for each material. When I look into the itab I get more then one entry per year per material.
e.g.:
Mat Year Sum
123 2007 100
123 2007 009
What am I doing wrong, I thought the collect sums it up?
chris
‎2008 Jan 10 4:05 PM
Hi Chris,
you have to put the "collect" into a loop, as it does not work on the entire table at once.
as in
loop at itab into wa.
collect wa into itab2.
endloop.you might also be able to get the desired result directly with the select by removing the "TABLE" addition and adding ENDSELECT.
Cheers
Thomas
‎2008 Jan 10 4:03 PM
SELECT matnr mjahr erfmg
INTO CORRESPONDING FIELDS OF itab1
FROM mseg
WHERE bwart = '501'.
COLLECT itab1.
ENDSELECT.
‎2008 Jan 10 4:05 PM
Hi Chris,
you have to put the "collect" into a loop, as it does not work on the entire table at once.
as in
loop at itab into wa.
collect wa into itab2.
endloop.you might also be able to get the desired result directly with the select by removing the "TABLE" addition and adding ENDSELECT.
Cheers
Thomas
‎2008 Jan 10 4:05 PM
HI,
Write the select statement in between select and endselect if you are using collect statement.
Or
Once you get the data into itab, do as below :
Create another internal table and work area same as itab.
loop at itab into wa.
collect wa to itab2.
endloop.
Thanks,
Sriram Ponna.
‎2008 Jan 10 4:09 PM
Just as a side note, you may want to reconsider getting rid of the into corresponding fields and just send this into table Itab. This will help you in performance.
‎2008 Jan 10 4:16 PM
stat. collect sums up all curr or quantities fields and it should be with in the loop.
it checks each and every record, if it matches with same record, its sums the curr/quantities to same record.
SELECT matnr mjahr erfmg
INTO CORRESPONDING FIELDS OF itab1
FROM mseg
WHERE bwart = '501'.
COLLECT itab1.
ENDSELECT.