-1

I am trying to summarise specific fields in my table that are based on a system table ekbe. Here is how I started:

REPORT zz_program3.
TABLES: ekbe.

TYPES: BEGIN OF tt_mati1,
ebeln LIKE ekbe-ebeln,
ebelp LIKE ekbe-ebelp,
bewtp LIKE ekbe-bewtp,
bwart LIKE ekbe-bwart,
menge LIKE ekbe-menge,
suma1 TYPE i,
END OF tt_mati1.

DATA: lt_mati1 TYPE STANDARD TABLE OF tt_mati1 WITH KEY elebn ebelp.

SELECT *
FROM ekbe
INTO CORRESPONDING FIELDS OF TABLE lt_mati1
WHERE ( bewtp = 'E' AND bwart = '101' ).

Now, I want to sum all fields that will occur, for example:

EBELN EBELP BEWTP BWART MENGE
450001122 01 E 101 5,000
450001122 01 E 101 1,000
450001122 01 E 101 1,000
450001122 02 E 101 1,000
450001122 02 E 101 2,000

And I would like to have something like this:

EBELN EBELP BEWTP BWART MENGE SUMA1
450001122 01 E 101 5,000 7,000
450001122 01 E 101 1,000 7,000
450001122 01 E 101 1,000 7,000
450001122 02 E 101 1,000 3,000
450001122 02 E 101 2,000 3,000

I know I have to use FOR GROUPS and REDUCE functions but unfortunately I do not know how to implement this in internal table based on system table.

Can anyone help me with solving?

Sandra Rossi
  • 11,934
  • 5
  • 22
  • 48
matthewkun
  • 1
  • 1
  • 2
  • 2
    The ABAP documentation and the Web is full of examples, so please try to search a little bit. You may start with [ABAP documentation](https://help.sap.com/doc/abapdocu_latest_index_htm/latest/en-US/index.htm?file=abenfor_grouping_abexas.htm). – Sandra Rossi Jun 08 '22 at 06:41
  • You can do this with a GROUP BY clause in your SQL statement. I could show you how, but I would need to know which field of the table you want to sum up. – Philipp Jun 08 '22 at 13:36
  • also check this https://stackoverflow.com/questions/57920458/new-abap-syntax-instead-of-collect/57937966#57937966 – Suncatcher Jun 08 '22 at 14:46

1 Answers1

1

You don't need to use REDUCE or FOR GROUPS for this. You can do grouping within your SQL statement by using a GROUP BY clause.

SELECT 
  ebeln,
  ebelp
  SUM( menge ) AS menge,
  SUM( dmbtr ) AS suma1,
FROM ekbe
INTO CORRESPONDING FIELDS OF TABLE lt_mati1
WHERE ( bewtp = 'E' AND bwart = '101' )
GROUP BY ebeln, ebelp.

This will take all rows with the same values in EBELN and EBELP and crunch them into one row each with the sum of their MENGE and DMBTR.

Philipp
  • 67,764
  • 9
  • 118
  • 153