‎2009 Sep 02 8:46 AM
hi all,
i have a doubt regarding AT NEW and ON CHANGE. i have an ITAB and i have to calculate subtotal with respect to MATNR..
WERKS MATNR MENGE
BILF AB 4
BILF AB 2
DLFF AB 6
DLFF XY 5
BILR XY 5
BILR EF 5
can anyone plz tell me that what shud i use AT NEW or ON CHANGE and which will trigger how much times?
Regards saurabh.
‎2009 Sep 02 9:13 AM
Hi Saurabh,
on change of will trigger 3 times..
when matnr is AB, XY,EF.
on change of is an obsolete control structure..
AT NEW will also consider the fields to the left of MATNR in the given structure.
it takes combination of WERKS MATNR & triggers 5 times..
BILF AB
DLFF AB
DLFF XY
BILR XY
BILR EF
Regards,
Mdi.Deeba
‎2009 Sep 02 8:49 AM
Hi,
In respect to the internal table structure that you had provided try like below:
loop at itab.<<<same structure and data as you provided
at end of matnr.
sum.
write:/ itab-mantr, itab-menge. <<<this will write the subtotal for the material.
endat.
endloop.
Regards,
Himanshu
‎2009 Sep 02 8:52 AM
Hi,
AT END Or AT NEW trigeres when any field (including tat field) on the left of that field changes.
That is in ur case if u use at new it wil get trigered if werks or matnr changes..
but that is not ur requirement..
so for ur requirement u need to use on change of matnr..
but on change is obsolete..
so wht u can do is chagne the order of the internal table such that matnr becomes the first field. and then use AT NEW or AT END
Regards
Ansari
‎2009 Sep 02 9:09 AM
HI,
MAKE use of AT-NEW.
SORT the internal table by werks and matnr and then use AT NEW.
But within AT NEW the MENGE will have value as '*' in the work area.
So before AT NEW pass the contents into a separate work area.
You can make use of COLLECT statement instead.
Sort your internal table on the basis of werks and matnr.
Then LOOP on it and COLLECT into another internal table of the same type.
Regards,
Ankur Parab
‎2009 Sep 02 9:12 AM
You can use At new or At end statement in this situation. But you need to keep one this in mind if you are workin on ECC then at new or at end statement chnage all the content on workarea as ***** so avoid this situation either use field symbols or take another workarea and move the content of orignal workarea into the new one and use this new work area fro printing.
Loop at itab assign to <wa_itab>.
at new.....
endat.
endloop.
Loop at itab into wa_itab.
wa_itab1 = wa_itab.
at new.......
use the value of wa_itab1 to avoid *****
endat.
endloop
Also keep the thing suggested by Ansari in mind while using these statements.
You can also use collect statement, collect statement sum all the numeric fields on the basis on key fields. For more information on collect see F1 help.
‎2009 Sep 02 9:13 AM
Hi Saurabh,
on change of will trigger 3 times..
when matnr is AB, XY,EF.
on change of is an obsolete control structure..
AT NEW will also consider the fields to the left of MATNR in the given structure.
it takes combination of WERKS MATNR & triggers 5 times..
BILF AB
DLFF AB
DLFF XY
BILR XY
BILR EF
Regards,
Mdi.Deeba
‎2009 Sep 02 9:22 AM
Hi,
<li> If you want to have subtotal of MATNR , You should not use AT NEW and ON CHANGE OF. ON CHANGE OF is obsolete.
<li> You need to use AT END OF statement.
<li> Before that , Table needs to be sorted.
<li>Try this way
Thanks
Venkat.O
DATA: BEGIN OF it_marc OCCURS 0,
werks TYPE marc-werks,
matnr TYPE marc-matnr,
eisbe TYPE marc-eisbe,
mabst TYPE marc-mabst,
END OF it_marc.
START-OF-SELECTION.
SELECT * FROM marc INTO CORRESPONDING FIELDS OF TABLE it_marc UP TO 2000 ROWS.
SORT it_marc BY werks matnr.
LOOP AT it_marc.
AT END OF matnr.
SUM.
WRITE:/ it_marc-werks, it_marc-matnr, it_marc-eisbe, it_marc-mabst.
ENDAT.
ENDLOOP.