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

Former Member
0 Kudos

Hi all,

The requirement is to add the quantity field if the first two fields matnr and werks are same.

can we use comparing f1 and f2 in the collect statement.

if not? how i have to do.

Thanks,

raina

9 REPLIES 9

Former Member
0 Kudos

Hi,

Are these two only character fields.. Refer to this..

The following statement allows you to summate entries in an internal table:

COLLECT <wa> INTO <itab>.

<itab> must have a flat line type, and all of the fields that are not part of the table key must have a numeric type (F, I, or P). You specify the line that you want to add in a work area that is compatible with the line type.

Regards,

Tanveer

Please mark helpful answers

Former Member
0 Kudos

HI suresh,

1. The internal table in which u collect,

should have

on the LEFT MOST,

only matnr and werks.

2. rest the system will automatically sum

up based on their distinct combination.

regards,

amit m.

Former Member
0 Kudos

Suresh,

COLLECT will total up the numeric fields if the character fields of the internal table are same. I am not sure in your table if you have char fields other than matnr and werks. If yes and the data differs there, collect will not work.

If you have only 3 fields, then whenever matnr and werks are same collect will sum up the qty field, no clauses required In fact you cannot specify any conditions.

Regards,

Ravi

Note : Please mark the helpful answers

former_member188685
Active Contributor
0 Kudos

Hi,

if you have only the matnr,werks,volum then you can use collect it will work. but if you have some other fields they differ then collect will fail.

try to take one temp table and move only matnr,werks,volum and then perform collect on the temp table.

Regards

vijay

Former Member
0 Kudos

Hi raina,

i do it on this way:

data: begin of itab occurs 0,

matnr like marc-matnr,

werks like marc-werks,

i type i,

end of itab.

*

select * from marc.

itab-matnr = marc-matnr.

itab-werks = marc-werks.

i = 1.

collect itab.

endselect.

*

loop at itab.

write: / itab-matnr, itab-werks, itab-i.

endloop.

Hope this can help you,

Regards, Dieter

Former Member
0 Kudos

HI

GOOD

YOU HAVE NOT GIVEN THE CODE FOR YOUR REQUIREMENT ,IN WHICH TABLE YOU WANT TO ADD THE QUANTITY FIELD.

YOU CAN DO LIKE THIS ALSO YOU CAN COMPARE BETWEEN TWO FIELDS MATNR AND WERKS USING IF LOOP IF BOTH ARE SAME THAN STORE THE QUANTITY FIELD IN A ANOTHER VARIABLE AND APPEND THAT FIELD TO YOUR INTERNAL TABLE.

THANKS

MRUTYUN

Former Member
0 Kudos
Hi suresh,

   If collect doesnt work u can go for control breaks
put ur first field as MATNR and second field as WERKS and the rest of the fields next.

declare 2 similar internal tables itab and itab1.

loop at itab.
  at end of matnr werks.
     SUM.
   move-corresponding itab to itab1.
   append itab1.
   clear itab1.
  endat.
endloop.

Former Member
0 Kudos

Hai Suresh B

Go through this document

Basic form

COLLECT [wa INTO] itab.

Addition

... SORTED BY f

Effect

COLLECT is used to create unique or compressed datsets. The key fields are the default key fields of the internal table itab .

If you use only COLLECT to fill an internal table, COLLECT makes sure that the internal table does not contain two entries with the same default key fields.

If, besides its default key fields, the internal table contains number fields (see also ABAP/4 number types ), the contents of these number fields are added together if the internal table already contains an entry with the same key fields.

If the default key of an internal table processed with COLLECT is blank, all the values are added up in the first table line.

If you specify wa INTO , the entry to be processed is taken from the explicitly specified work area wa . If not, it comes from the header line of the internal table itab .

After COLLECT , the system field SY-TABIX contains the index of the - existing or new - table entry with default key fields which match those of the entry to be processed.

Notes

COLLECT can create unique or compressed datasets and should be used precisely for this purpose. If uniqueness or compression are unimportant, or two values with identical default key field values could not possibly occur in your particular task, you should use APPEND instead. However, for a unique or compressed dataset which is also efficient, COLLECT is the statement to use.

If you process a table with COLLECT , you should also use COLLECT to fill it. Only by doing this can you guarantee that

the internal table will actually be unique or compressed, as described above and

COLLECT will run very efficiently.

If you use COLLECT with an explicitly specified work area, it must be compatible with the line type of the internal table.

Example

Compressed sales figures for each company

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.

The table COMPANIES now has the following appearance:

NAME SALES

Duck 40

Tiger 20

Addition

... SORTED BY f

Effect

COLLECT ... SORTED BY f is obsolete and should no longer be used. Use APPEND ... SORTED BY f which has the same meaning.

Note

Performance

The cost of a COLLECT in terms of performance increases with the width of the default key needed in the search for table entries and the number of numeric fields with values which have to be added up, if an entry is found in the internal table to match the default key fields.

If no such entry is found, the cost is reduced to that required to append a new entry to the end of the table.

A COLLECT statement used on a table which is 100 bytes wide and has a key which is 60 bytes wide and seven numeric fields is about approx. 50 msn (standardized microseconds).

Note

Runtime errors

COLLECT_OVERFLOW : Overflow in integer field when calculating totals.

COLLECT_OVERFLOW_TYPE_P : Overflow in type P field when calculating totals.

Thanks & regards

Sreenivasulu P

Former Member
0 Kudos

Hi Suresh,

Please find the following code, it may be useful to u.

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.

<i>Reward Points If It Helps YOU.</i>

Regards,

Raghavendra