Application Development and Automation 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: 
Read only

sum in internal table

Former Member
0 Likes
3,677

hi all,

I've an internal table with fields

matnr ,

quantity ,

received_week ,

required_week.

I want to sum quantities where values of matnr, received_week, required_week are same.

any suggestions please.. if possible please send me sample code.

thanks in advance.

Message was edited by:

sapsudha rao

hi all,

I've an internal table with fields

matnr ,

quantity ,

received_week ,

required_week.

I want to sum quantities where values of matnr, received_week, required_week are same.

any suggestions please.. if possible please send me sample code.

thanks in advance.

Message was edited by:

sapsudha rao

12 REPLIES 12
Read only

Former Member
0 Likes
1,450

use "COLLECT" statement.

Award if it helps.

Read only

Former Member
0 Likes
1,450

i've been tried collect ..but it's not working...

Read only

0 Likes
1,450

Hi,

you can loop at your table and sum up the entries for qunatity.

like loop at tabname.

v_quant = v_quant + (quantity field).

endloop.

Read only

Former Member
0 Likes
1,450

hi,

use cloolect statement as

collect [internaltablename].

if useful rewards ome points.

reagards,

suresh babu.

Read only

alex_m
Active Contributor
0 Likes
1,450

Collect statement should work, can u place your code.

Read only

Former Member
0 Likes
1,450

hi Alexander,

herewith I'm sending my code.

data : begin of itab occurs 0,

mandt like zcusreq-mandt,

maktx like makt-maktx,

updat like zcusreq-updat,

kunnr like zcusreq-kunnr,

matnr like zcusreq-matnr,

reqwk like zcusreq-reqwk,

menge like zcusreq-menge,

recwk like zcusreq-recwk,

fname like zcusreq-fname,

end of itab.

data : begin of itab1 occurs 0,

kunnr like zcusreq-kunnr,

matnr like zcusreq-matnr,

reqwk like zcusreq-reqwk,

menge like zcusreq-menge,

recwk like zcusreq-recwk,

end of itab1.

select updat kunnr matnr reqwk menge recwk

into corresponding fields of table itab

from zcusreq where matnr in s_mtnr and

reqwk in s_week.

loop at itab.

move-corresponding itab to itab1.

collect itab1.

endloop.

But it's not working.

Message was edited by:

sapsudha rao

Message was edited by:

sapsudha rao

Read only

Former Member
0 Likes
1,450

hi,

while declaring fieldcatalogs.

d_fieldcat_wa-fieldname = 'MATNR'.

d_fieldcat_wa-seltext_l = 'material number'.

d_fieldcat_wa-col_pos = 1.

append d_fieldcat_wa to d_fieldcat.

clear d_fieldcat_wa.

d_fieldcat_wa-fieldname = 'RECIEVED_WEEK'.

d_fieldcat_wa-seltext_l = 'recieved week'.

d_fieldcat_wa-col_pos = 2.

append d_fieldcat_wa to d_fieldcat.

clear d_fieldcat_wa.

d_fieldcat_wa-fieldname = 'REQUIRED_WEEK'.

d_fieldcat_wa-seltext_l = 'required week'.

d_fieldcat_wa-col_pos = 3.

append d_fieldcat_wa to d_fieldcat.

clear d_fieldcat_wa.

d_fieldcat_wa-fieldname = 'QUANTITY'.

d_fieldcat_wa-seltext_l = 'QUANTITY'.

d_fieldcat_wa-col_pos = 1.

d_fieldcat_wa-do_sum = 'X'.

append d_fieldcat_wa to d_fieldcat.

clear d_fieldcat_wa.

call function module reuse_alv_grid_display.

call function module reuse_alv_grid_display.

exporting.

program name = gd_repid.

t_fieldcatalog = d_fieldcat.

importing.

t_outtab = itab.

exceptions.

Reward with points if helpful.

Message was edited by:

Vinutha YV

Read only

Former Member
0 Likes
1,450

try this,

if u want to material wise total than

loop ai itab.

at end of matnr.

sum.

write:/ quantity.

endat.

endloop.

otherwise simple use

loop at itab.

sum.

endloop.

reward if useful

regards,

Imran

Read only

Former Member
0 Likes
1,450

Hi,

Use control break statements.

Ex.

1)

DATA: BEGIN OF LINE,

COL1 TYPE C,

COL2 TYPE I,

COL3 TYPE I,

END OF LINE.

DATA ITAB LIKE HASHED TABLE OF LINE

WITH UNIQUE KEY COL1 COL2.

LINE-COL1 = 'A'.

DO 3 TIMES.

LINE-COL2 = SY-INDEX.

LINE-COL3 = SY-INDEX ** 2.

INSERT LINE INTO TABLE ITAB.

ENDDO.

LINE-COL1 = 'B'.

DO 3 TIMES.

LINE-COL2 = 2 * SY-INDEX.

LINE-COL3 = ( 2 * SY-INDEX ) ** 2.

INSERT LINE INTO TABLE ITAB.

ENDDO.

SORT ITAB.

LOOP AT ITAB INTO LINE.

WRITE: / LINE-COL1, LINE-COL2, LINE-COL3.

AT END OF COL1.

SUM.

ULINE.

WRITE: / LINE-COL1, LINE-COL2, LINE-COL3.

SKIP.

ENDAT.

AT LAST.

SUM.

ULINE.

WRITE: / LINE-COL1, LINE-COL2, LINE-COL3.

ENDAT.

ENDLOOP.

The output is:

A 1 1

A 2 4

A 3 9

________________________________

A 6 14

B 2 4

B 4 16

B 6 36

________________________________

B 12 56

________________________________

  • 18 70

Ex.2)

REPORT DEMO.

NODES: SPFLI, SFLIGHT, SBOOK.

FIELD-GROUPS: HEADER, FLIGHT_INFO, FLIGHT_BOOKING.

INSERT:

SPFLI-CITYFROM SPFLI-CITYTO

SPFLI-CONNID SFLIGHT-FLDATE

SBOOK-CLASS SBOOK-SMOKER SBOOK-BOOKID INTO HEADER,

SPFLI-CARRID INTO FLIGHT_INFO,

SBOOK-LUGGWEIGHT SBOOK-WUNIT INTO FLIGHT_BOOKING.

START-OF-SELECTION.

GET SPFLI.

EXTRACT FLIGHT_INFO.

GET SFLIGHT.

GET SBOOK.

EXTRACT FLIGHT_BOOKING.

END-OF-SELECTION.

SORT.

LOOP.

AT FLIGHT_INFO.

SKIP.

WRITE: / SPFLI-CARRID,

SPFLI-CONNID,

'from', (15) SPFLI-CITYFROM,

'to', (15) SPFLI-CITYTO.

ULINE.

ENDAT.

AT NEW SFLIGHT-FLDATE.

SKIP.

WRITE: / 'Date:', SFLIGHT-FLDATE.

WRITE: 20 'Book-ID', 40 'Smoker', 50 'Class'.

ULINE.

ENDAT.

AT FLIGHT_BOOKING.

WRITE: / SBOOK-BOOKID UNDER 'Book-ID',

SBOOK-SMOKER UNDER 'Smoker',

SBOOK-CLASS UNDER 'Class'.

ENDAT.

AT END OF SFLIGHT-FLDATE.

ULINE.

WRITE: 'Number of bookings: ', (3) CNT(SBOOK-BOOKID),

/ 'Total luggage weight:',

SUM(SBOOK-LUGGWEIGHT), SBOOK-WUNIT.

ENDAT.

ENDLOOP.

Regards,

Bhaskar

Read only

Former Member
0 Likes
1,450

Collect will not work because :

Collect will work like an alternate to Append but it will sum up all the non characteristic fields.

Example : if itab has 3 fields name, mark1,mark2.

after filling itab work area then the statement collect itab will add the mark1,mark2 fields for identical name values in the tabel.

if table is : abc 10 20

def 30 40

now collect itab with values abc 20 30 .. the table will become

abc 30 50

def 30 40

For your problem, it is bit simple : we can keep where condition like .

clear mat_sum.

Loop at itab where require_date > dat1.

mat_sum = mat_sum + material

endif.

Try it all the very best to you .

Regards,

-Mohan.

Read only

Former Member
0 Likes
1,450

hi rao

will you please send your sum in internal table

matnr

quantity

received_week

thx in advance

sat

Read only

Former Member
0 Likes
1,450

hi

      try this.

data : begin of itab occurs 0,

            matnr  type matnr,

            quantity type kwmeng,

            received_week type c,

            required_week  type c,         

            END of itab .

collect itab.

this general works

else try this.

data : begin of gtab ,

            matnr  type matnr,

            quantity type kwmeng,

            received_week type c,

            required_week  type c,         

            END of gtab .

data itab type table of gtab occurs 0 with header line.

data itab1 type table of gtab occurs 0 with header line.

select * into corresponding fields of table itab.

sort matnr,received_week,required_week.

loop at itab.

if  matnr1 ne itab-matnr and

    received_week1 ne itab-received_week and

     required_week1 ne itab-required_week.

    clear itab1-quantity.

     itab1-matnr = itab-matnr .

    itab1-received_week = itab-received_week.

     itab1-required_week = itab-required_week.

    itab1-quanttity = itab-quanttity.

    

     matnr1 = itab-matnr .

    received_week1 = itab-received_week.

     required_week1 = itab-required_week.

     append itab1 .

    elase.

     itab1-matnr = itab-matnr .

    itab1-received_week = itab-received_week.

     itab1-required_week = itab-required_week.

    itab1-quanttity =   itab1-quanttity + itab-quanttity.

    

     update  itab1.

   endif.

enloop.

regards if helpfull .

( Is this a way to avoid "Reward if  helpful"? .If your intention is to request for points, please refrain from doing so).

Message was edited by: Venkat Gowrishankar