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: 

Collect statement

Former Member
0 Kudos

Hi sap gurus,

I have a table with fields:

A B C D E

Row1 10 X 30 Y

Row1 10 X 15 Y

Row2 14 2 2 2

Row3 1 1 1 1

I have a struc with fields A B D.

Now i want to compress this table based on this struc.

So that my final output is:

A B C D E

Row1 20 X 45 Y

Row2 14 2 2 2

Row3 1 1 1 1

1 ACCEPTED SOLUTION

Former Member
0 Kudos

just loop at ur table that is with entries and at end of A use collect atatement to sum up all the quantity fields or u can also sum alll the fields in at end of - endat and then transfer them to ur new structure

reward if helpful

7 REPLIES 7

Former Member
0 Kudos

just loop at ur table that is with entries and at end of A use collect atatement to sum up all the quantity fields or u can also sum alll the fields in at end of - endat and then transfer them to ur new structure

reward if helpful

Former Member
0 Kudos

Hi,

Use collect statement...See the example.

DATA: BEGIN OF LINE,

COL1(3) TYPE C,

COL2(2) TYPE N,

COL3 TYPE I,

END OF LINE.

DATA ITAB LIKE SORTED TABLE OF LINE

WITH NON-UNIQUE KEY COL1 COL2.

LINE-COL1 = 'abc'. LINE-COL2 = '12'. LINE-COL3 = 3.

COLLECT LINE INTO ITAB.

WRITE / SY-TABIX.

LINE-COL1 = 'def'. LINE-COL2 = '34'. LINE-COL3 = 5.

COLLECT LINE INTO ITAB.

WRITE / SY-TABIX.

LINE-COL1 = 'abc'. LINE-COL2 = '12'. LINE-COL3 = 7.

COLLECT LINE INTO ITAB.

WRITE / SY-TABIX.

LOOP AT ITAB INTO LINE.

WRITE: / LINE-COL1, LINE-COL2, LINE-COL3.

ENDLOOP.

Pls. reward if useful....

Former Member
0 Kudos

hi,

do this way ..


sort itab by a.
loop at itab.
 collect itab. 
endloop.

0 Kudos

Collect:

When the line is inserted, the system checks whether there is already a table entry that matches the key. If there is no corresponding entry already in the table, the COLLECT statement has the same effect as inserting the new line. If an entry with the same key already exists, the COLLECT statement does not append a new line, but adds the contents of the numeric fields in the work area to the contents of the numeric fields in the existing entry.

You should only use the COLLECT statement if you want to create summarized tables. If you use other statements to insert table entries, you may end up with duplicate entries.

asik_shameem
Active Contributor
0 Kudos

Hi,

try this..

Note that field B and D in itab have to of type I or P.

LOOP AT itab.
  COLLECT itab INTO itab_new.
ENDLOOP.

GayathriRR
Product and Topic Expert
Product and Topic Expert
0 Kudos

Hi guys,

Please note that my struct doesnt hav all the fields of the table..So in this case how will my collect look like

venkat_o
Active Contributor
0 Kudos

Gayathri, Check the program. It givs the way u expected.



REPORT zvenkat_notepad.

DATA:
      BEGIN OF itab OCCURS 0,
        a TYPE char10,
        b TYPE i,
        c TYPE char2,
        d TYPE i,
        e TYPE char2,
      END OF itab,
      BEGIN OF w_data,
        a TYPE char10,
        b TYPE i,
        d TYPE i,
      END OF w_data.

START-OF-SELECTION.

  w_data-a = 'row1'.
  w_data-b = 10.
  w_data-d = 30.
  MOVE-CORRESPONDING w_data TO itab.
  itab-c = 'X'.
  itab-e = 'Y'.
  COLLECT itab.
  CLEAR  itab.

  w_data-a = 'row1'.
  w_data-b = 10.
  w_data-d = 15.
  MOVE-CORRESPONDING w_data TO itab.
  itab-c = 'X'.
  itab-e = 'Y'.
  COLLECT itab.
  CLEAR  itab.

  w_data-a = 'row2'.
  w_data-b = 14.
  w_data-d = 2.
  MOVE-CORRESPONDING w_data TO itab.
  itab-c = '2'.
  itab-e = '2'.
  COLLECT itab.
  CLEAR  itab.

  w_data-a = 'row3'.
  w_data-b = 1.
  w_data-d = 1.
  MOVE-CORRESPONDING w_data TO itab.
  itab-c = '1'.
  itab-e = '1'.
  COLLECT itab.
  CLEAR  itab.

  LOOP AT itab .
    WRITE: / itab-a,
            itab-b,
            itab-c,
            itab-d,
            itab-e.
  ENDLOOP.
I hope that it solves ur problem. Regards, Venkat.O