在循环内的相同字段中求和值

时间:2017-06-20 07:32:12

标签: sap abap

在我要做的程序中,显示每个公司代码的信息。 例如:

Fiscal Year | 4030   | 4020  | 4040 | 4050 | 4070 | TOTAL
1/2010      | 423.12 | 89.79 ..... | ......|  .... | .....
2/2010      |234.00  | 04.38 ..... | ...... | ....  |.....
3/2010      |432.652 | 98.80 ..... | ...... | .... | .....
.
.
12/2010     |978.687 | 089.787 .... | ...... | .... |  .....

我的问题是如何将字段fkdat中的所有数据与日期和dmbtr中的数据相加?因为在字段fkdat中有很多相同的日期。我想要做的就是总结所有相同的日期和金额,以便我可以在每个工厂代码中显示信息和金额。我怎么能在循环中做到这一点?

SELECT-OPTIONS:
BUKRS for wa_zfi_vbrp_bseg_1-BUKRS OBLIGATORY NO INTERVALS.
PARAMETERS:
 p_gjahr LIKE zfi_vbrp_bseg-gjahr OBLIGATORY.
SELECTION-SCREEN BEGIN OF LINE.
    SELECTION-SCREEN COMMENT 10(15) text-005.
    SELECTION-SCREEN POSITION 35.
    select-options: p_perde for ce1osgc-perde no-display.
SELECTION-SCREEN END OF LINE.
START-OF-SELECTION.
  SELECT 
        zf~gjahr
        zf~bukrs
        zf~dmbtr
        zf~monat
        zf~vbeln
        zf~hkont
        fi~fkdat
        vb~werks
    INTO CORRESPONDING FIELDS OF TABLE it_zfi_vbrp_bseg_1
    FROM zfi_vbrp_bseg as zf
    INNER JOIN vbrp as vb ON vb~vbeln EQ zf~vbeln
    INNER JOIN zfi_vbrp as fi ON zf~vbeln EQ fi~vbeln
    WHERE zf~bukrs in bukrs
    AND zf~gjahr EQ p_gjahr
    GROUP BY
        zf~gjahr
        zf~bukrs
        zf~dmbtr
        zf~monat
        zf~vbeln
        zf~hkont
        fi~fkdat
        "zf-perio
        vb~werks.
"-------------------------------------------------------------------------------------------------------------
DATA:" l_budat TYPE budat,
        date TYPE string,
        date1 TYPE string,
        date2 TYPE STRING,
        out TYPE string,
        vvdcs TYPE ce1osgc-vvdcs.
DATA: pos TYPE i VALUE 40,
      pos2 TYPE i VALUE 57,
      pos3 TYPE i VALUE 75,
      pos4 TYPE i VALUE 93,
      pos5 TYPE i VALUE 110.
 IF SY-SUBRC EQ 0.
  LOOP AT it_zfi_vbrp_bseg_1 INTO wa_zfi_vbrp_bseg_1.
    AT FIRST.
         WRITE: /01(150) SY-ULINE,
                 /01  SY-VLINE,
                  11  'Fiscal Year',  36  SY-VLINE,
                  40  '4030',         55  SY-VLINE,
                  57  '4020',         73 SY-VLINE,
                  75  '4050',         91 SY-VLINE,
                  93  '4040',         108 SY-VLINE,
                  110 '4070',         127 SY-VLINE,
                  129 'Total',        140 SY-VLINE,
                  (150) SY-ULINE.
     ENDAT.
IF wa_zfi_vbrp_bseg_1-werks  EQ '4030'.
   WRITE AT pos wa_zfi_vbrp_bseg_1-dmbtr.
ENDIF.
IF wa_zfi_vbrp_bseg_1-werks  EQ '4020'.
      WRITE AT pos2 wa_zfi_vbrp_bseg_1-dmbtr.
ENDIF.
IF wa_zfi_vbrp_bseg_1-werks  EQ '4050'.
       WRITE AT pos3 wa_zfi_vbrp_bseg_1-dmbtr.
ENDIF.
IF wa_zfi_vbrp_bseg_1-werks  EQ '4040'.
        WRITE AT pos4 wa_zfi_vbrp_bseg_1-dmbtr.
ENDIF.
IF wa_zfi_vbrp_bseg_1-werks  EQ '4070'.
     WRITE AT pos5 wa_zfi_vbrp_bseg_1-dmbtr.
ENDIF.
ENDLOOP.
ENDIF.

1 个答案:

答案 0 :(得分:2)

尝试在循环中使用COLLECT

检查如何使用COLLECT声明:

LOOP AT it_zfi_vbrp_bseg_1 INTO wa_zfi_vbrp_bseg_1.
  COLLECT wa_zfi_vbrp_bseg_1 INTO it_zfi_vbrp_bseg_2. " You have to declear a new internal table.
ENDLOOP.

LOOP AT it_zfi_vbrp_bseg_2 INTO wa_zfi_vbrp_bseg_1.
  WRITE Causes......
  .........
ENDLOOP.