‎2009 May 13 12:08 PM
Hello Experts,
My requirement is that i have one internal table from which if date is same for suppose 2 records,
that corresponding columns get added.
Can i use colllect statement for that.
PLz suggest.
Ravi
‎2009 May 13 12:17 PM
Hi, Ravi,
Following Sample Code will help you in this way.
TYPES: BEGIN OF ty_it,
key TYPE i,
amount TYPE i,
END OF ty_it.
DATA: it TYPE STANDARD TABLE OF ty_it WITH HEADER LINE WITH KEY key.
DATA: it2 TYPE STANDARD TABLE OF ty_it WITH HEADER LINE WITH KEY key.
it-key = 1. it-amount = 12. APPEND it.
it-key = 1. it-amount = 12. APPEND it.
it-key = 1. it-amount = 12. APPEND it.
it-key = 2. it-amount = 12. APPEND it.
LOOP AT it.
COLLECT it INTO it2.
ENDLOOP.
LOOP AT it2.
WRITE: / it2-key, it2-amount.
ENDLOOP.Best Regards,
Faisal
‎2009 May 13 12:10 PM
Yes, you can use the collect statement 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
‎2009 May 13 12:13 PM
If you have only date as key field you can use COLLECT to add all the numeric fields. But if you have a key field other than date it will only collect for lines which is having similiar key fields.
Eg:-
Date Qty
20090513 100
20090513 200
After Collect u will get
20090513 300
But for the following scenario
Date Co.Code Qty
20090513 IN01 100
20090513 IN02 200
After collect the output will be same because the key fields are different
‎2009 May 13 12:14 PM
Hi,
Prerequisite for the use of collect is that work area is compatible with the row type of internal table to which we are appending the data and all components that are not part of the table key must have a numeric data type (i, p, f).
So if there is only date column and rest all the column are of data type (i, p ,f) in your internal table then you can go for collect. Else its better you use the control statement AT New and At end of.
Regards,
Nikhil
‎2009 May 13 12:15 PM
‎2009 May 13 12:17 PM
Hi, Ravi,
Following Sample Code will help you in this way.
TYPES: BEGIN OF ty_it,
key TYPE i,
amount TYPE i,
END OF ty_it.
DATA: it TYPE STANDARD TABLE OF ty_it WITH HEADER LINE WITH KEY key.
DATA: it2 TYPE STANDARD TABLE OF ty_it WITH HEADER LINE WITH KEY key.
it-key = 1. it-amount = 12. APPEND it.
it-key = 1. it-amount = 12. APPEND it.
it-key = 1. it-amount = 12. APPEND it.
it-key = 2. it-amount = 12. APPEND it.
LOOP AT it.
COLLECT it INTO it2.
ENDLOOP.
LOOP AT it2.
WRITE: / it2-key, it2-amount.
ENDLOOP.Best Regards,
Faisal
‎2009 May 13 12:17 PM
Hi,
Yes you can use it for the Quantity and Currency fields.
For that..
data: itab type standard table of structure1.
data: ctab type standard table of structure1.
data: wa type structure1.
fill the itab with select query.
now..
loop at itab into wa.
collect wa into ctab.
clear wa.
endloop.Thats it, Now your ctab contains required data.
Thanks,
Naveen I
‎2009 May 13 12:21 PM
HI,
To know how collect statement works, refer to this link..http://help.sap.com/saphelp_nw70/helpdata/en/fc/eb36d5358411d1829f0000e829fbfe/content.htm