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 not working for table fields

Former Member
0 Likes
1,346

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.

1 ACCEPTED SOLUTION
Read only

karuna_gangireddy
Contributor
0 Likes
1,211

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.

9 REPLIES 9
Read only

karuna_gangireddy
Contributor
0 Likes
1,212

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.

Read only

0 Likes
1,211

Hi,

Yes, this would probably work.

However, is there any reason why my original code is not working?

Read only

0 Likes
1,211

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

Read only

0 Likes
1,211

I've just put it as the 1st field. However, it's still not working. Frustrating, since I've used sum before.

Read only

0 Likes
1,211

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.

Read only

0 Likes
1,211

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..

Read only

brad_bohn
Active Contributor
0 Likes
1,211

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...

Read only

Former Member
0 Likes
1,211

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.

Read only

brad_bohn
Active Contributor
0 Likes
1,211

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.