7 Replies Latest reply: Feb 20, 2008 6:59 AM by Lakshmanan Balasubramanian RSS

Sum of one column of an internal table

Pavan Sanganal
Currently Being Moderated

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

  • Re: Sum of one column of an internal table
    Madhavi t
    Currently Being Moderated

    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

  • Re: Sum of one column of an internal table
    Nehru Pandidurai Sinnachamy
    Currently Being Moderated

    Hi,

     

    Ucan use control brack stmt like this .

     

     

    AT FIRST.

    ...

    ENDAT.

    AT NEW.

    ....

    ENDAT.

    AT END.

    ....

    ENDAT.

    AT LAST

    ....

    ENDAT.

     

     

    Regards.

    S.Nehru

  • Re: Sum of one column of an internal table
    Narendra Maddipati
    Currently Being Moderated

    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

  • Re: Sum of one column of an internal table
    praveen parasiya
    Currently Being Moderated

    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

  • Re: Sum of one column of an internal table
    Karthik Thiruvenkadam
    Currently Being Moderated

    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
    
    
    

     

  • Re: Sum of one column of an internal table
    Jyothsna M
    Currently Being Moderated

    Hi

    use control break statements

    AT END OF kunnr.

                SUM.

           ENDAT.

  • Re: Sum of one column of an internal table
    Lakshmanan Balasubramanian
    Currently Being Moderated

    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

Actions