Skip to Content
author's profile photo Former Member
Former Member

Difference between sum and collect

I want exact difference between sum and collect with any ex.

Add a comment
10|10000 characters needed characters exceeded

Assigned Tags

Related questions

4 Answers

  • Best Answer
    author's profile photo Former Member
    Former Member
    Posted on Dec 02, 2007 at 06:50 AM

    Hi,

    Sum:

    You can only use this statement within a LOOP. If you use SUM in an AT - ENDAT block, the system calculates totals for the numeric fields of all lines in the current line group and writes them to the corresponding fields in the work area. If you use the SUM statement outside an AT - ENDAT block (single entry processing), the system calculates totals for the numeric fields of all lines of the internal table in each loop pass and writes them to the corresponding fields of the work area. It therefore only makes sense to use the SUM statement in AT...ENDAT blocks. If the table contains a nested table, you cannot use the SUM statement. Neither can you use it if you are using a field symbol instead of a work area in the LOOP statement.

    Collect:

    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.

    You can also go through this:

    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 (see also ABAP/4 number types ) 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 (see SORT ), 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.

    ******************************************************************

    SUM.

    Effect

    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 (see also ABAP number types) and places them in the LOOP output area (header line of the internal table or an explicitly specified work area).

    You can use the SUM statement both at the end and the beginning of a control group (see also AT FIRST/LAST).

    Example

    Display the table T with sub-totals:

    TYPES: BEGIN OF T_TYPE,

    CODE(4),

    SALES TYPE P,

    DISCOUNT TYPE P,

    END OF T_TYPE.

    DATA: T TYPE STANDARD TABLE OF T_TYPE WITH NON-UNIQUE

    DEFAULT KEY INITIAL SIZE 100,

    WA_T TYPE T_TYPE.

    ...

    LOOP AT T INTO WA_T.

    AT FIRST.

    SUM.

    WRITE: /4 'Grand Total:',

    20 WA_T-SALES, 40 WA_T-DISCOUNT.

    ULINE. SKIP.

    ENDAT.

    WRITE: / WA_T-CODE,

    20 WA_T-SALES, 40 WA_T-DISCOUNT.

    AT END OF CODE.

    SUM.

    WRITE: / WA_T-CODE, 10 'Total:',

    20 WA_T-SALES, 40 WA_T-DISCOUNT.

    SKIP.

    ENDAT.

    ENDLOOP.

    Example

    Summarized sales figures by company:

    COLLECT

    Effect

    COLLECT allows you to create unique or summarized datasets. The system first tries to find a table entry corresponding to the table key. (See also Defining Keys for Internal Tables). 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 ( ABAP Numeric Types).

    If the system finds an entry, the numeric fields that are not part of the table key (see ABAPNumeric Types) 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 kind of the internal table:

    TYPES: BEGIN OF COMPANY,

    NAME(20) TYPE C,

    SALES TYPE I,

    END OF COMPANY.

    DATA: COMP TYPE COMPANY,

    COMPTAB TYPE HASHED TABLE OF COMPANY

    WITH UNIQUE KEY NAME.

    COMP-NAME = 'Duck'. COMP-SALES = 10. COLLECT COMP INTO COMPTAB.

    COMP-NAME = 'Tiger'. COMP-SALES = 20. COLLECT COMP INTO COMPTAB.

    COMP-NAME = 'Duck'. COMP-SALES = 30. COLLECT COMP INTO COMPTAB.

    Table COMPTAB now has the following contents:

    NAME | SALES

    -


    Duck | 40

    Tiger | 20

    http://techpreparation.com/computer-interview-questions/sap-abap-interview-questions-answers3.htm

    <b>Reward if usefull</b>

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Dec 01, 2007 at 11:55 AM

    Hi..

    Sum will add the whole internal values and store in the first line of the internal

    table..

    Collect will add the common key values in the internal table and store in the corresponding key field.

    Regards

    Bala..

    Add a comment
    10|10000 characters needed characters exceeded

  • Posted on Dec 01, 2007 at 11:42 AM
    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Dec 01, 2007 at 01:09 PM

    sum will add all d values n store at single field .

    collect will add all numeric values of same character fields.it means if char fields same bt numeric valus are same then it wil add numeric vales n store in single record.

    ex:

    itab:

    record1: abc xyz klm 20 40 60.

    rd 2: abc xyz klm 10 30 50.

    collect itab.

    then. d o/p: abc xyz klm 30 70 110.

    thats it.

    Add a comment
    10|10000 characters needed characters exceeded

Before answering

You should only submit an answer when you are proposing a solution to the poster's problem. If you want the poster to clarify the question or provide more information, please leave a comment instead, requesting additional details. When answering, please include specifics, such as step-by-step instructions, context for the solution, and links to useful resources. Also, please make sure that you answer complies with our Rules of Engagement.
You must be Logged in to submit an answer.

Up to 10 attachments (including images) can be used with a maximum of 1.0 MB each and 10.5 MB total.