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: 

Help to do Sum

Former Member
0 Kudos

Hi,

i Have internal table with fields like in e.g. below

and i wont to sum the Work days and put it in the last Colman

what is the Best way to do that?

Regards

i have this table without Colman sum_total (empty) .

what i wont is to spread the sum for all appearance of pernr in sum total Colman :

pernr      kostl       work_days        sum_total
 
111             5555             5                   13
111             5555             8                   13
444             6666             10                  15
444             6666             2                   15
444             6666             3                   15
555             5656             3                    6
555             5656             3                    6

1 ACCEPTED SOLUTION

Former Member
0 Kudos

Hi

You can try the following way.

data: begin of itab occurs 0,

pernr type char3,

ktosl type char4,

workd type i,

sumwr type i,

end of itab.

data: it_out like itab occurs 0 with header line.

itab-pernr = '111'.

itab-ktosl = '5555'.

itab-workd = 5.

append itab.

itab-pernr = '111'.

itab-ktosl = '5555'.

itab-workd = 8.

append itab.

itab-pernr = '444'.

itab-ktosl = '6666'.

itab-workd = 10.

append itab.

itab-pernr = '444'.

itab-ktosl = '6666'.

itab-workd = 2.

append itab.

itab-pernr = '444'.

itab-ktosl = '6666'.

itab-workd = 3.

append itab.

itab-pernr = '555'.

itab-ktosl = '5656'.

itab-workd = 3.

append itab.

itab-pernr = '555'.

itab-ktosl = '5656'.

itab-workd = 3.

append itab.

loop at itab.

collect itab into it_out.

clear it_out.

endloop.

clear itab.

loop at it_out.

itab-pernr = it_out-pernr.

itab-ktosl = it_out-ktosl.

itab-sumwr = it_out-workd.

modify itab transporting sumwr where pernr eq it_out-pernr

and ktosl eq it_out-ktosl.

endloop.

loop at itab.

write : itab-pernr, itab-ktosl, itab-workd, itab-sumwr, /.

endloop.

Reward points, if it is helpful.

Regards

Raja.

5 REPLIES 5

Former Member
0 Kudos

hi check this...i hope we can do like this...

loop at itab.

at end of pernr.

sum .

v_total = sum .

endat.

v_total = sum + v_total.

endloop.

Former Member
0 Kudos

Hi

You can try the following way.

data: begin of itab occurs 0,

pernr type char3,

ktosl type char4,

workd type i,

sumwr type i,

end of itab.

data: it_out like itab occurs 0 with header line.

itab-pernr = '111'.

itab-ktosl = '5555'.

itab-workd = 5.

append itab.

itab-pernr = '111'.

itab-ktosl = '5555'.

itab-workd = 8.

append itab.

itab-pernr = '444'.

itab-ktosl = '6666'.

itab-workd = 10.

append itab.

itab-pernr = '444'.

itab-ktosl = '6666'.

itab-workd = 2.

append itab.

itab-pernr = '444'.

itab-ktosl = '6666'.

itab-workd = 3.

append itab.

itab-pernr = '555'.

itab-ktosl = '5656'.

itab-workd = 3.

append itab.

itab-pernr = '555'.

itab-ktosl = '5656'.

itab-workd = 3.

append itab.

loop at itab.

collect itab into it_out.

clear it_out.

endloop.

clear itab.

loop at it_out.

itab-pernr = it_out-pernr.

itab-ktosl = it_out-ktosl.

itab-sumwr = it_out-workd.

modify itab transporting sumwr where pernr eq it_out-pernr

and ktosl eq it_out-ktosl.

endloop.

loop at itab.

write : itab-pernr, itab-ktosl, itab-workd, itab-sumwr, /.

endloop.

Reward points, if it is helpful.

Regards

Raja.

Former Member
0 Kudos

Hi Another simple way of doing this is using two internal tables.

lets say ur output table as you have shown is gi_out.

declare another table gi_pernr same structure as of gi_out.


gi_pernr[] = gi_out[]
sort gi_pernr by pernr.
delete adjacent duplicates from gi_pernr comparing pernr.

loop at gi_pernr into gw_pernr.
clear lv_days.
loop at gi_out into gw_out where pernr = gw_pernr-pernr.
lv_days = lv_days + gw_out-work_days.
endloop.

modify gi_out from gw_out transporting sum_total where pernr = gw_pernr-pernr.
endloop.

.

This will surely work..

thnx,

Ags..

Former Member
0 Kudos

Hi,

U can code like----


SORT ITAB BY PERNR.

LOOP AT itab.

AT END OF KOSTL.

SUM.

WRITE: / itab-PERNR, itab-KOSTL, itab-TOL.

ENDAT.

ENDLOOP.

Reward if helpful.

Thanks.

kesavadas_thekkillath
Active Contributor
0 Kudos

sort it by pernr kostl.
data:sum_val type p.
loop at itab.
at new pernr.
sum.
sum_val = itab-work_days.
endat.
itab-sum_total = sum_val.
modify itab transporting sum_total where pernr = itab-pernr
				   and kostl = itab-kostl.
endloop.