Application Development and Automation 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: 
Read only

collect statement

Former Member
0 Likes
1,303

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

1 ACCEPTED SOLUTION
Read only

faisalatsap
Active Contributor
0 Likes
1,266

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

7 REPLIES 7
Read only

Former Member
0 Likes
1,266

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

Read only

Former Member
0 Likes
1,266

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

Read only

Former Member
0 Likes
1,266

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

Read only

Former Member
0 Likes
1,266

Yes....

You can use it..

Read only

faisalatsap
Active Contributor
0 Likes
1,267

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

Read only

naveen_inuganti2
Active Contributor
0 Likes
1,266

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

Read only

Former Member
0 Likes
1,266

HI,

To know how collect statement works, refer to this link..http://help.sap.com/saphelp_nw70/helpdata/en/fc/eb36d5358411d1829f0000e829fbfe/content.htm