Register Login

Difference between Collect and Sum in SAP ABAP

Updated May 18, 2018

SUM vs. Collect in SAP ABAP

SUM

When processing an internal table in a block starting with LOOP and concluded by ENDLOOP, SUM calculates the control totals of all fields of type I, F and P and places them in the LOOP output area (header line of the internal table or an explicitly specified work area).

When you use SUM in a LOOP with an explicitly specified output area, this output area must be compatible with the line type of the internal table. When using LOOP to process a sorted extract, the control total of f at the end of the group appears in the field SUM(f) - - if f is type I, F or P.

COLLECT

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,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.

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.

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.

If the system finds an entry, the numeric fields that are not part of the table key are added to the sum total of the existing entries. If it does not find an entry, the system creates a new entry instead.

The way in which the system finds the entries depends on the type of the internal table:

STANDARD TABLE:

The system creates a temporary hash administration for the table to find the entries. This means that the runtime required to find them does not depend on the number of table entries. The administration is temporary, since it is invalidated by operations like DELETE, INSERT, MODIFY, SORT, ...). A subsequent COLLECT is then no longer independent of the table size, because the system has to use a linear search to find entries. For this reason, you should only use COLLECT to fill standard tables.

SORTED TABLE:

The system uses a binary search to find the entries. There is a logarithmic relationship between the number of table entries and the search time.


×