‎2009 Dec 09 8:44 PM
Hi,
I have an internal table that contains over 10 fields.
One of those fields is the activity type. I have several activity types, sorted descending.
Each record with an activity type also contains a field with a currency.
As such, for all unique activity types in my table, I am trying to sum the currency.
However, the following code isn't working. No sum is being created for unique the unique combination of activity types:
SORT I_COST1 BY LSTAR DESCENDING.
I_COST2[] = I_COST1[].
LOOP AT I_COST2.
AT END OF LSTAR.
SUM.
ENDAT.
ENDLOOP.
What am I doing wrong. Please help.
‎2009 Dec 09 9:15 PM
Hi,
Try doing something like this:
SORT I_COST1 BY LSTAR DESCENDING.
I_COST2[] = I_COST1[].
LOOP AT I_COST2.
LOOP AT I_COST1 WHERE I_COST1-LSTAR = I_COST2-LSTAR.
LS_ I_COST1-CURRENCY = LS_I_COST1-CURRENCY + I_COST1-CURRENCY.
ENDLOOP.
MODIFY I_COST1 FROM LS_I_COST1 TRANSPORTING CURRENCY WHERE LSTAR = I_COST2-LSTAR.
ENDLOOP.
P{lease let me know if you still have any issue?
Regards,
Karuna.
‎2009 Dec 09 9:15 PM
Hi,
Try doing something like this:
SORT I_COST1 BY LSTAR DESCENDING.
I_COST2[] = I_COST1[].
LOOP AT I_COST2.
LOOP AT I_COST1 WHERE I_COST1-LSTAR = I_COST2-LSTAR.
LS_ I_COST1-CURRENCY = LS_I_COST1-CURRENCY + I_COST1-CURRENCY.
ENDLOOP.
MODIFY I_COST1 FROM LS_I_COST1 TRANSPORTING CURRENCY WHERE LSTAR = I_COST2-LSTAR.
ENDLOOP.
P{lease let me know if you still have any issue?
Regards,
Karuna.
‎2009 Dec 09 9:19 PM
Hi,
Yes, this would probably work.
However, is there any reason why my original code is not working?
‎2009 Dec 09 9:27 PM
You should check the help for SUM and AT END. Basically, for this to work, LSTAR has to be the first field of the internal table.
Rob
‎2009 Dec 09 9:29 PM
I've just put it as the 1st field. However, it's still not working. Frustrating, since I've used sum before.
‎2009 Dec 09 9:46 PM
What isn't working though? Do you mean the value isn't correct in the work area or that you're expecting the table to retain the SUM somehow? You need to 'do something' (write it out, store it in another table, etc.) with the sum in order to retain it.
‎2009 Dec 09 9:51 PM
I just built a small program to test this.
Along with your reply, I just had one of those "oy" moments, where you realize just how stupid you are.
Thanks for the replies everyone..
‎2009 Dec 09 9:18 PM
Post your internal table definition. Did you exclude some code from the post or are you just looking at the results in debug mode? I'm not sure whether you're referring to a non-unique sum because your table isn't set up correctly, or because the field isn't typed correctly, or because there's no handling code after the 'SUM'? Most likely you're expecting something to happen with that SUM command other than just putting the SUM'd value in the work area...
‎2009 Dec 09 9:21 PM
The following is the table definition:
TYPES: BEGIN OF S_COST1,
SPTAG TYPE S227-SPTAG,
WERKS TYPE S227-WERKS,
MATNR TYPE S227-MATNR,
MAKTX TYPE MAKT-MAKTX,
MCOMP TYPE S227-MCOMP,
LSTAR TYPE S227-LSTAR,
KTEXT TYPE CSLT-KTEXT,
VERID TYPE S227-VERID,
HWAER TYPE S227-HWAER,
WERTN TYPE S227-WERTN,
AMEIN TYPE S225-AMEIN,
WAMNG TYPE S225-WEMNG,
AUFNR TYPE AUFK-AUFNR,
AUART TYPE AUFK-AUART,
LOEKZ TYPE CKMLMV013-LOEKZ,
VNPRD_EA TYPE CKMLCR-VNPRD_EA,
VNPRD_MA TYPE CKMLCR-VNPRD_MA,
ML_COST TYPE CK_VNPRDEA.
TYPES: END OF S_COST1.
This is the only relative code for this question. I am expecting to receive a sum.
There is no handling code after this relating to sum. However, via tutorials, I have found that there doesn't have to be.
‎2009 Dec 09 9:28 PM
So, your AT is going to trigger too many times for the same LSTAR and you won't receive a unique sum. You'll receive a sum based on any of the fields changing to the left of LSTAR.