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: 

Sum of one column of an internal table

Former Member
0 Kudos

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

1 ACCEPTED SOLUTION

Former Member
0 Kudos

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

7 REPLIES 7

Former Member
0 Kudos

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

Former Member
0 Kudos

Hi,

Ucan use control brack stmt like this .

AT FIRST.

...

ENDAT.

AT NEW.

....

ENDAT.

AT END.

....

ENDAT.

AT LAST

....

ENDAT.

Regards.

S.Nehru

Former Member
0 Kudos

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

Former Member
0 Kudos

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

Former Member
0 Kudos

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


Former Member
0 Kudos

Hi

use control break statements

AT END OF kunnr.

SUM.

ENDAT.

Former Member
0 Kudos

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