user7806296
user7806296

Reputation: 1

How can I sum up or collect the data on the same field?

Hi ABAP users I would like to ask if what process I can make to COLLECT the data on the same field? all I want to do is to sum up or collect the data in dmbtr that belongs to same date, (date field monat) (werks to plant codes)

it_zfi_vbrp_bseg_1-num3 = it_zfi_vbrp_bseg_1-werks.

it_zfi_vbrp_bseg_1-num2 = it_zfi_vbrp_bseg_1-dmbtr.

COLLECT it_zfi_vbrp_bseg_1.

DELETE ADJACENT DUPLICATES FROM it_zfi_vbrp_bseg_1 COMPARING ALL FIELDS.

Sort it_zfi_vbrp_bseg_1 by werks.

LOOP AT it_zfi_vbrp_bseg_1 into wa_zfi_vbrp_bseg_1 WHERE monat = '01'.

    IF wa_zfi_vbrp_bseg_1-werks EQ '4030'.

        WRITE:/, AT pos wa_zfi_vbrp_bseg_1-dmbtr. 
             
    ENDIF.
    
ENDLOOP.

Any configuration in my codes guys?

Upvotes: 0

Views: 17865

Answers (2)

Suncatcher
Suncatcher

Reputation: 10621

Suppose, you already extended standard bseg table with monat field, then you should do like this:

TYPES: BEGIN OF ty_zfi_vbrp_bseg_1,
        werks TYPE bseg-werks,
        monat TYPE monat,
        dmbtr TYPE bseg-dmbtr,
       END OF ty_zfi_vbrp_bseg_1.

DATA: it_zfi_vbrp_bseg_1 TYPE TABLE OF ty_zfi_vbrp_bseg_1,
      is_zfi_vbrp_bseg_1 TYPE ty_zfi_vbrp_bseg_1.

SELECT werks, monat, dmbtr
INTO TABLE @DATA(lt_bseg)
FROM bseg
WHERE werks = '4030'.

* summation of months
LOOP AT lt_bseg ASSIGNING FIELD-SYMBOL(<fs_line>).
  CLEAR: is_zfi_vbrp_bseg_1.
  MOVE-CORRESPONDING <fs_line> TO is_zfi_vbrp_bseg_1.
  COLLECT is_zfi_vbrp_bseg_1 INTO it_zfi_vbrp_bseg_1.
ENDLOOP.

* output the results
LOOP AT it_zfi_vbrp_bseg_1 ASSIGNING FIELD-SYMBOL(<zfi_line>).
    IF <zfi_line>-werks EQ '4030'.
        WRITE: / <zfi_line>-werks, <zfi_line>-monat, <zfi_line>-dmbtr.
    ENDIF.
ENDLOOP.

Couple of notes to your incorrect code:

  1. COLLECT doesn't work like single statement and should be executed in loop.
  2. To sum with COLLECT statement you should declare work area so that all non-key fields would be numeric. Key fields (even if it is implicit key) can be of any type, as opposed to what Ray said. N type is also OK.
  3. DELETE ADJACENT DUPLICATES is redundant here, because after COLLECT (which makes summation by primary key) you won't have any dups.

Upvotes: 2

Ray Mannion
Ray Mannion

Reputation: 19

This all comes down to how you define your internal table it_zfi_vbrp_bseg_1

For COLLECT to work, you need to define it with character type keys followed by packed field types:

data: begin of it_zfi_vbrp_bseg_1 occurs 0, werks type werks, month(2) type c, dmbtr type dmbtr, end of it_zfi_vbrp_bseg_1.

That will work.

This will not:

data: begin of it_zfi_vbrp_bseg_1 occurs 0. include structure vbrp. data: monat type monat, dmbtr type dmbtr, end of it_zfi_vbrp_bseg.

Read the help on COLLECT and define your summary table accordingly.

Upvotes: -1

Related Questions