Application Development 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: 

How to sum data based on a condition into a new field

0 Kudos

Hi

Guys please can you help . I need to create new fields based on data pulled from field CKIS-KOSTL. then i require the data to be sum up based on condition CKIS-KOSTL the values to be sum is in CKIS-WERTN.

How do I create a new field "1350" and "1380" and sum up data from CKIS-WERTN based on condition CKIS-KOSTL which has cost centres "1350" & "1380".

*& Report  Z_COST_MATERIAL5
REPORT  z_cost_material5 LINE-SIZE 250.
* TABLE DECLARATION
TYPES: BEGIN OF z_keph,
      matnr TYPE mbew-matnr,
      BWKEY TYPE mbew-bwkey,
      KST001 TYPE KEPH-KST001,
      kst004  TYPE keph-kst004,
      KST006 TYPE keph-kst006,
      KST010 TYPE keph-kst010,
      kalnr TYPE keph-kalnr,
      KKZST TYPE KEPH-KKZST,
      KSTAR TYPE CKIS-KSTAR,
      WERTN TYPE CKIS-WERTN,
      LSTAR TYPE CKIS-LSTAR,
      KADKY TYPE KEPH-KADKY,
      KOSTL TYPE CKIS-KOSTL.
TYPES:END OF z_keph.
 DATA: it_keph TYPE STANDARD TABLE OF z_keph INITIAL SIZE 0,
wa_keph TYPE z_keph,     "work area (header line)
wa_keph1 LIKE LINE OF it_keph.

SELECT-OPTIONS:
material FOR WA_KEPH-MATNR,
COSTINGD FOR WA_KEPH-KADKY.

*START OF SELECTION "TO INPUT DATA INTO INTERNAL TABLE IT_KEPH
START-OF-SELECTION.
  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.
*PROCESSING DATA IN IT_ZEPH
  LOOP AT it_keph INTO WA_KEPH.
    AT FIRST.
    WRITE:  'MATERIAL N0' COLOR 3,
           30 'DHU' COLOR 3,
            146 '1350' COLOR 3,
            156 '1380' COLOR 3.
     ULINE.
     ENDAT.
    WRITE: / WA_KEPH-MATNR,WA_KEPH-KST001,WA_KEPH-KST004,WA_KEPH-KST006,WA_KEPH-KST010,WA_KEPH-KSTAR,WA_KEPH-WERTN,WA_KEPH-LSTAR,WA_KEPH-KOSTL.
  ENDLOOP.
6 REPLIES 6

FredericGirod
Active Contributor
0 Kudos

if you have access to new Abap statement, you could play with REDUCE

https://blogs.sap.com/2017/05/25/replace-the-loop-for-reduce-operator/

IN
Contributor
0 Kudos

Hello,

You can try COLLECT statement. Find the document for COLLECT.

Regards,

Igor

FredericGirod
Active Contributor

for collect you need to create a table with qty/amount & the key. Otherwise it will sum everything

IN
Contributor
0 Kudos

I am aware of that. 🙂 Thought it would be the easiest solution without too much coding. 😄

Jelena
Active Contributor
0 Kudos

It's not clear what the summation is needed for exactly. If the goal is to output data on screen then instead of WRITE use SALV, which has built-in total/subtotal functionality.

DoanManhQuynh
Active Contributor
0 Kudos

First question is: do you want to sum value only based on KOSTL? what about other fields? If the summation condition is other fields also, you should use SELECT SUM( ) GROUP BY...

If your requirement is really sum up based on KOSTL, then the second question is: you only need cost center: 1350, 1380? why its not included in your where clause?

Using control level processing is one way, although the right statement should be AT END KOSTL but it sum up using KOSTL and the left components as bellow descriptions: (and have limitation of character length if i remember correctly):

Addition 2 
... {NEW}|{END OF} compi 
 
 Effect 
Control levels are defined by the beginning or end of a group of rows with the same content in the component compi (where i = 1, 2, and so on) and in the components to the left of compi. The content of these components determines the control key. The control breaks take place when the content of the component compi or another component to the left of compi changes. 

So LOOP AT GROUP BY could be your option here. you create a new field by adding it in type z_kepth declaration then loop, sum and populate to sum field.