04-21-2008 5:17 AM
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
04-21-2008 5:21 AM
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
04-21-2008 5:21 AM
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
04-21-2008 5:24 AM
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....
04-21-2008 5:24 AM
hi,
do this way ..
sort itab by a.
loop at itab.
collect itab.
endloop.
04-21-2008 6:06 AM
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.
04-21-2008 6:23 AM
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.
04-21-2008 6:44 AM
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
04-21-2008 7:28 AM
Gayathri,
Check the program. It givs the way u expected.
I hope that it solves ur problem.
Regards,
Venkat.O
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.