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

ERROR WHEN USING SUM

Former Member
0 Likes
2,999

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.

11 REPLIES 11
Read only

IN
Contributor
0 Likes
2,846

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

Read only

Former Member
0 Likes
2,846

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.

Read only

IN
Contributor
0 Likes
2,846

There are different ways of achieving the goal, dependant of your needs.

  1. The recommendation of satishkumarbalasubramanian
  2. Through the loop statement by keeping the sum into the local internal table with totals for the cost centre and fetching the end result in the end or after every row.

Regards,

Igor

Read only

former_member1716
Active Contributor
0 Likes
2,846

zubair sultan,

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

Read only

0 Likes
2,846

zubair sultan,

Can you let us know your select query based on that we can suggest.

Regards

Read only

0 Likes
2,846

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

0 Likes
2,846

zubair sultan,

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.

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

Read only

DoanManhQuynh
Active Contributor
0 Likes
2,846

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.

Read only

0 Likes
2,846

I just selected one has a example I do need all cost centre.

Read only

0 Likes
2,846

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.

Read only

2,846

zubair sultan

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.