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

Regarding SUM

Former Member
0 Likes
654

Hi All,

The Scenario is i have the internal table with the fallowing data

GPID Name Invoice_ID Total_Amount Subsidy_Amount

123456 david 1234567 12344 12356

132564 john 2536498 32568 12456

123456 david 135648 36958 5689

here if we have Same GPID then i need to sum the record and append this record into another internal table as single record as

GPID Name Invoice_ID Total_Amount Subsidy_Amount

123456 david 1234567/135648 49302 18045

132564 john 2536498 32568 12456

Like this i need to do, here i have done

LOOP AT GT_DATA INTO LS_DATA.

MOVE: LS_DATA-GPID TO LS_DATA1-GPID,

LS_DATA-TP_NAME TO LS_DATA1-TP_NAME,

LS_DATA-INV_ID TO ls_data1-inv_id.

AT END OF GPID.

SUM .

MOVE: LS_DATA-TOT_INV_AMOUNT TO LS_DATA1-TOT_INV_AMOUNT,

LS_DATA-TOT_SUB_AMOUNT TO LS_DATA1-TOT_SUB_AMOUNT.

APPEND LS_DATA1 TO GT_data1.

ENDAT.

ENDLOOP.

but with this code the invoice_id i am getting as a single value only but i required as mentioned above 1234567/135648.

please correct my code.

7 REPLIES 7
Read only

Former Member
0 Likes
621

Try as following

LOOP AT GT_DATA INTO LS_DATA.
MOVE: LS_DATA-GPID         TO LS_DATA1-GPID,
            LS_DATA-TP_NAME TO LS_DATA1-TP_NAME,
            LS_DATA-INV_ID      TO ls_data1-inv_id,
            LS_DATA-TOT_INV_AMOUNT TO LS_DATA1-TOT_INV_AMOUNT,
            LS_DATA-TOT_SUB_AMOUNT TO LS_DATA1-TOT_SUB_AMOUNT.
  COLLECT LS_DATA1 INTO GT_data1.
ENDLOOP.

Read only

0 Likes
621

Hi,

If i use Collect statement it will sum the Invoice Numbers also.

i need the Invoice Numbers to Display as 123456/25638 as two invoice numbers if we collect the two records.

regards,

kumar

Read only

0 Likes
621

Ok, so you want to concatenate all invoice_id of the same group_id and sum the value.

It's a bit strange, as the invoice_id field will have to be of an undetermined size, so you better use a char type field.

As for the code, it's not as clear cut as that.


loop at table1.
  read table2 with key group_id = table1-group_id.
  if sy-subrc = 0.
    concatenate table2-invoice_id '/' table1-invoice_id into table2-invoice_id.
    table2-value = table2-value + table1-value.
    modify table2.
  else.
     table2-group_id = table1-group_id.
     table2-invoice_id = table1-invoice_id.
     table2-value = table1-value.
     append table2.
  endif.
endloop.

Something like this will do.

Read only

0 Likes
621

Hi,

If i use Collect statement it will sum the Invoice Numbers also.

i need the Invoice Numbers to Display as 123456/25638 as two invoice numbers if we collect the two records.

regards,

kumar

Read only

0 Likes
621

Hi,

Not sure if you noticed my earlier message, this shud work i suppose.


LOOP AT GT_DATA INTO LS_DATA.
MOVE: LS_DATA-GPID TO LS_DATA1-GPID,
LS_DATA-TP_NAME TO LS_DATA1-TP_NAME,
LS_DATA-INV_ID TO ls_data1-inv_id.
CONCATENATE  GT_DATA-INVOICE_ID '/' TEMP_VAR INTO TEMP_VAR.
AT END OF GPID.
SUM .
MOVE: LS_DATA-TOT_INV_AMOUNT TO LS_DATA1-TOT_INV_AMOUNT,
LS_DATA-TOT_SUB_AMOUNT TO LS_DATA1-TOT_SUB_AMOUNT.
MOVE TEMP_VAR into LS_DATA-invoice_id.
APPEND LS_DATA1 TO GT_data1.
CLEAR TEMP_VAR.
ENDAT.
ENDLOOP.

THanks

Mani

Read only

Former Member
0 Likes
621

Hi Kumar,

Keep concatneating the invoice_id separated by a / into a temporary variable. This statement should be a line above AT END OF GPID.

After SUM with the move statements move this temp variable to the LS_DATA-invoice_id.

After Append statement, clear the temporary variable.

Hope this helps.

Thanks

Mani

Read only

PedroGuarita
Active Contributor
0 Likes
621

Instead of using at end and append, use collect only. That way, records with the same key will be summed in the new table.