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 according to certain fields in the internal table

Former Member
0 Kudos
8,352

Hi experts,

my question is how can I apply collect statement according to certain fields.

I have an internal table with six fields (f1,f2...f6). I want to collect according to f2 and f6, no matter what is the value of the other fields

4 REPLIES 4

Former Member
0 Kudos
1,441

hi,

COLLECTstatement works only if the field are not char type (C,N,P) and compare the value of the fields with char type to form the group in which the values are to be added.

So, make sure,

1. your f2 and f6 will be of type i,f.

2.the fields w.r.to which you are operating COLLECT operation should be of type (C,N,P).

for further detail check,

http://help.sap.com/saphelp_nw70/helpdata/en/fc/eb36d5358411d1829f0000e829fbfe/content.htm.

regards,

anirban

Former Member
0 Kudos
1,441

DATA: BEGIN OF COMPANIES OCCURS 10,

NAME(20),

SALES TYPE I,

END OF COMPANIES.

COMPANIES-NAME = 'Duck'.

COMPANIES-SALES = 10.

COLLECT COMPANIES.

COMPANIES-NAME = 'Tiger'.

COMPANIES-SALES = 20.

COLLECT COMPANIES.

COMPANIES-NAME = 'Duck'.

COMPANIES-SALES = 30.

COLLECT COMPANIES.

loop at companies.

write:/ companies-name,

companies-sales.

endloop.

Former Member
0 Kudos
1,441

COLLECT allows you to create unique or summarized datasets. The system first tries to find a table entry corresponding to the table key. . The key values are taken either from the header line of the internal table itab, or from the explicitly-specified work area wa. The line type of itab must be flat - that is, it cannot itself contain any internal tables. All the components that do not belong to the key must be numeric types.

for examples

it_data-F1 = 'A'

it_data-F2 = 'B'

it_data-F3 = 12

it_data-F4 = 10

Collect.it_data.

Clear it_data.

this will append above line to it_data. But if

it_data-F1 = 'A'

it_data-F2 = 'C'

it_data-F3 = 12

it_data-F4 = 10

Collect.it_data.

Clear it_data.

this will also append above line to it_data. But if

it_data-F1 = 'A'

it_data-F2 = 'B'

it_data-F3 = 22

it_data-F4 = 15

Collect.it_data.

Clear it_data.

This entry aleady exit. So F3 & F4 will be added to same entry

and existing entry will becone

it_data-F1 = 'A'

it_data-F2 = 'B'

it_data-F3 = 10

it_data-F4 = 5

Hope this help you.

Rgds

rajesh

Former Member
0 Kudos
1,441

Hi,

By default, types C,N,D,T and X forms the key of internal table.

If you want only F1 and F2 as key fields of the table(based on which collect should work), Then try making them as key

when declaring the internal table.

Regards

Meenakshi