‎2019 Nov 19 1:14 PM
Hi all
I am trying to sum up field-WERTN when the condition is equal to '1380' and to input the sum value into field DHU1.please can you help me to correct the error below.

‎2019 Nov 19 1:40 PM
Hello,
Your code and logic doesn't look good. Can you be a liitle more precise what are you trying to achieve? Do you want to write only the rows that have the specific cost centre and get their sum or do you want to write all the rows and get the current total into the field DHU1 for specific cost centre?
Regards,
Igor
‎2019 Nov 19 2:01 PM
Hi
I will like to sum rows rows that have the specific cost centre and get their sum and input into dhu1.
eg: sum values for costcentre 1380 and input into a field called 1380 then sum values for cost centre 1340 input the total value into cost centre 1340.
‎2019 Nov 19 2:22 PM
There are different ways of achieving the goal, dependant of your needs.
Regards,
Igor
‎2019 Nov 19 2:11 PM
You can do this in a single query, you don't need to write loop.
Below code for example,
SELECT werks, datum, uzeit, extnb, deprt, dpext, SUM( amunt ) AS amunt
INTO CORRESPONDING FIELDS OF TABLE @gt_compr
FROM yrt_h_sales
WHERE werks IN @so_werks
AND datum IN @so_datum
GROUP BY werks, datum, uzeit, extnb, deprt, dpext.Just refer the above code and modify yours.
Regards
‎2019 Nov 19 2:14 PM
‎2019 Nov 19 2:27 PM
SPAN {
font-family: "Courier New";
font-size: 10pt;
color: #000000;
background: #FFFFFF;
}
.L0S33 {
color: #4DA619;
}
.L0S52 {
color: #0000FF;
}
.L0S55 {
color: #800080;
}
.L0S70 {
color: #808080;
}
SELECT: A~MATNR A~BWKEY B~KST001 B~KST004 B~KST006 B~KST010 B~KALNR C~KSTAR C~WERTN C~LSTAR C~KOSTL
FROM mbew AS a
JOIN keph AS b
ON a~kaln1 EQ b~kalnr INNER JOIN CKIS AS C
ON B~KALNR EQ C~KALNR
AND
B~KADKY EQ C~KADKY
INTO CORRESPONDING FIELDS OF TABLE it_keph
WHERE A~MATNR IN MATERIAL
and a~bwkey EQ '3000'
AND B~KKZST EQ 'X'
AND B~KADKY IN COSTINGD.
‎2019 Nov 19 3:33 PM
In this case you need to do use the control break statements concepts.
Refer to the below link to know more about control break statements.
Thumb rule in using these statements,
All the fields before the required field on which the rule is applied must be equal only then your code will be effective.
In case you are only concerned about the cost centre then move the cost centre to first field in your internal table.
Regards!!!
‎2019 Nov 20 1:03 AM
There is no such the way to SUM like that inside LOOP. use control break as I already said in this comment . I just don't get why you only write out the specific cost center but select all of them.
‎2019 Nov 20 1:05 AM
I just selected one has a example I do need all cost centre.
‎2019 Nov 20 1:16 AM
I do need all cost centres .my logic is sum up column CKIS-WERTN based on condition CKIS-KOSTL which is linked to cost estimate number CKIS-kalnr.
‎2019 Nov 20 7:20 AM
what you want is what ALV subtotal do, use ALV you don't have to sum up then write, ALV do it for you:
cl_salv_table=>factory(
IMPORTING
r_salv_table = salv
CHANGING
t_table = it_keph ).
salv->get_sorts( )->add_sort(
EXPORTING
columnname = 'KOSTL'
sequence = if_salv_c_sort=>sort_up
subtotal = if_salv_c_bool_sap=>true
).
salv->get_aggregations( )->add_aggregation(
EXPORTING
columnname = 'WERTN'
aggregation = if_salv_c_aggregation=>total ).
salv->get_aggregations( )->set_aggregation_before_items( ).
salv->display( ).
If you still want to stick with class WRITE statement, then use AT END KOSTL ( please move it to the top of structure instead last component ) to sum and write, press F1 help on that statement you will find SAP sample for it.