Skip to Content
avatar image
Former Member

Sum of one column of an internal table

Hi All,

I have an internal table with two columns.

First column is having standard taxt "ZSSS" (for all rows).

The second column is having Amount value.

Internal table might have multiple lines with this structure.

Now i would like to sum the second column which is having Amount value.

How to code to get the sum in such scenario?

Your help will be appreciated.....

Regards

Pavan

Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

7 Answers

  • Best Answer
    avatar image
    Former Member
    Feb 20, 2008 at 05:22 AM

    loop itab

    at last

    sum

    total_amount = itab-fld2.

    endat

    endloop.

    the sum value is available in work area or headerline of itab

    write: total_amount

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Feb 20, 2008 at 05:25 AM

    Hi

    This is possible with COntrol Break Stamnts\Check below Code for example..

    DATA: BEGIN OF line,

    carrid TYPE sbook-carrid,

    connid TYPE sbook-connid,

    fldate TYPE sbook-fldate,

    custtype TYPE sbook-custtype,

    class TYPE sbook-class,

    bookid TYPE sbook-bookid,

    END OF line.

    DATA itab LIKE SORTED TABLE OF line WITH UNIQUE KEY table_line.

    SELECT carrid connid fldate custtype class bookid

    FROM sbook INTO CORRESPONDING FIELDS OF TABLE itab.

    LOOP AT itab INTO line.

    AT FIRST.

    WRITE / 'List of Bookings'.

    ULINE.

    ENDAT.

    AT NEW carrid.

    WRITE: / 'Carrid:', line-carrid.

    ENDAT.

    AT NEW connid.

    WRITE: / 'Connid:', line-connid.

    ENDAT.

    AT NEW fldate.

    WRITE: / 'Fldate:', line-fldate.

    ENDAT.

    AT NEW custtype.

    WRITE: / 'Custtype:', line-custtype.

    ENDAT.

    WRITE: / line-bookid, line-class.

    AT END OF class.

    ULINE.

    ENDAT.

    ENDLOOP.

    Hope it helps.

    Praveen

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Feb 20, 2008 at 05:36 AM

    hi,

    U can Use 'AT' Function for checking Sum.. I'l give you a piece of code. Check to it..

    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
    
    
    

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Feb 20, 2008 at 05:19 AM

    use control break statements

    First sort the itab based on first column.

    loop at itab.

    <code>

    at end of <itab-field1>

    sum

    write: / <itab-field2>

    endat.

    endloop

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Feb 20, 2008 at 05:22 AM

    Hi,

    Ucan use control brack stmt like this .

    AT FIRST.

    ...

    ENDAT.

    AT NEW.

    ....

    ENDAT.

    AT END.

    ....

    ENDAT.

    AT LAST

    ....

    ENDAT.

    Regards.

    S.Nehru

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Feb 20, 2008 at 05:56 AM

    Hi

    use control break statements

    AT END OF kunnr.

    SUM.

    ENDAT.

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Feb 20, 2008 at 05:59 AM

    Hi Dude,

    Here is the answer for you Query,

    If you have the same ZSSS text in coumln 1 and amount value in column 2,to add this amount value ,use collect statement as below:

    loop at itab.

    collect itab.

    endloop.

    loop at itab.

    write: itab-amount."Contains the sum

    endloop.

    Regards,

    Lakshmanan

    Add comment
    10|10000 characters needed characters exceeded