10-27-2015 6:22 PM
Hi ABAP Colleagues
I have an internal table as follows:
ORDER COST ELEMENT AMOUNT
921188 601123 200
921188 601124 100
921188 601124 300
921188 601124 300
921188 601125 100
921188 601125 200
921189 601123 200
921189 601124 100
921189 601124 250
921189 601125 400
Within any order number, if the cost element 601125 occurs more than once, I need to append a record underneath that 601125 record which is a total of the preceeding 601125 records. If within any order number the cost element 601125 only occurs once, then do not append any record. So the above should look like this after processing:
ORDER COST ELEMENT AMOUNT
921188 601123 200
921188 601124 100
921188 601124 300
921188 601124 300
921188 601125 100
921188 601125 200
921188 601125 300 <<-- insert a record which is total of preceeding 2 601125 records
921189 601123 200
921189 601124 100
921189 601124 250
921189 601125 400 <<-- no need to insert/append a record here as only 1 601125 record exists.
Can you advise please how might I achieve this? We are not on 740 ABAP release.
Thanks and regards
10-27-2015 6:37 PM
Hi
Also when inserting a summary record under the 601125 records I need to blank out the target amount associated with preceeding 601125 records that have been summarized by the inserted 601125 record. See below.
ORDER COST ELEMENT AMOUNT TARGET
921188 601123 200 80
921188 601124 100 70
921188 601124 300 50
921188 601124 300 30
921188 601125 100 55
921188 601125 200 55
921189 601123 200 60
921189 601124 100 40
921189 601124 250 40
921189 601125 400 30
Within any order number, if the cost element 601125 occurs more than once, I need to append a record underneath that 601125 record which is a total of the preceeding 601125 records. If within any order number the cost element 601125 only occurs once, then do not append any record. So the above should look like this after processing:
ORDER COST ELEMENT TARGET AMOUNT
921188 601123 80 200
921188 601124 70 100
921188 601124 50 300
921188 601124 30 300
921188 601125 100 <--blank target amount
921188 601125 200 <--blank target amount
921188 601125 55 300 <-- insert record which is total of preceeding 2 * 601125 records
921189 601123 60 200
921189 601124 40 100
921189 601124 40 250
921189 601125 30 400 <<-- no need to insert/append a record here as only 1 601125 record exists.
Any help on how to achieve this would be appreciated.
Thank you.
10-28-2015 2:23 AM
Here is the sample code. I assumed that amount is not zero.
VBELN - Order
KSTAR - Cost element
TARG - Target
AMUNT - Amount
loop at lt_data into ls_data.
ls_data2 = ls_data.
append ls_data2 to lt_data2.
at end of kstar.
sum.
if ls_data-kstar = 601125 and ls_data-amunt <> ls_data2-amunt.
append ls_data to lt_data2.
endif.
endat.
endloop.
lt_data[] = lt_data2[].
loop at lt_data2 assigning <fs_data> where kstar = 601125.
read table lt_data into ls_data
index sy-tabix + 1.
if sy-subrc = 0.
if ls_data-vbeln = <fs_data>-vbeln and ls_data-kstar = <fs_data>-kstar.
ls_data2 = <fs_data>.
clear <fs_data>-targ.
else.
<fs_data>-targ = ls_data2-targ.
endif.
endif.
endloop.
Do you want to have this only for some specific Cost element? If not then just delete the code where you see 601125.
-Chandra
10-28-2015 4:23 PM
Hi Chandra
Yes I think this will work well, thanks for the idea!
Regards
Michael
10-28-2015 5:17 PM
It it solves, please mark the answers as Helpful/Answered and close the discussion.
10-28-2015 2:54 AM
Hi Michael,
Sort the internal table by order and cost center, Create a another internal table.
data it_sum like itab.
loop at itab into wa.
collect wa into it_sum. "Values are summed.
endloop.
loop at itab into wa.
read table it_sum into wa_sum with key order = wa-order
cost center = wa-costcenter.
check sy-subrc is initial.
if wa_sum-amount NE wa-amount. "Checking the amount
Append wa_Sum to itab.
endif.
endloop.
Hope it helpful,
Regards,
Venkat.
10-28-2015 6:26 AM
Hello, can you using at end of COST ELEMENT field, fist you have to sort it_tab by ORDER, COST ELEMENT. and then at end of COST ELEMENT you can check previewous records if same, than you can do sum else you can skip and continue ( using sy-tabix - 1 )
Hope it can help