简体   繁体   中英

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?

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM