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

Help in Sum

Former Member
0 Likes
1,675

Hi,

i Have in program internal table with fields like below

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

what is the Best way to do that?

Regards

i have this table without Colman sum (empty) .

pernr      orgeh       sick_days        sum
 
123        5555            3                   7
123        5555            4                   7
456        6666            7                  16
456        6666            1                  16
456        6666            8                  16
789        5656            3                  6
789        5656            3                  6

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
1,652

hi ricardo...

ur modify statement is not correct.

LOOP AT e_tab INTO wa_e_tab.

LOOP AT e_tab INTO wa_e_tab2.

IF wa_e_tab-penrnr EQ wa_e_tab2-penrnr.

sum = sum + wa_e_tab-sick_days.

MOVE sum TO wa_e_tab-sum.

ELSE.

sum = 0.

ENDIF.

MODIFY e_tab FROM wa_e_tab TRANSPORTING sum .

ENDLOOP.

ENDLOOP.

instead of wa_e_tab use wa_e_tab2.... this is because ur still in the internal loop. so only this wa will affect the rows with same penrnr. use a where condition in this also. like

MODIFY e_tab FROM wa_e_tab2 TRANSPORTING sum where wa_e_tab-penrnr EQ wa_e_tab2-penrnr

i hope this helps.

15 REPLIES 15
Read only

Former Member
0 Likes
1,652

hi there....

loop at this internal table, initiate the variable which will hold the sum as 0, and then do as follows

loop at itab.

sum = sum + sick_days.

endloop.

write 😕 sum.

what will happen is that the loop will start off from the first record till the last and will keep on adding the number of sick days into sum. Hence, at the end of the loop, u will hav the total number of sick days.

i hope this answers ur query. Do reward if helpful or get back with further issues.

Read only

0 Likes
1,652

Hi prem ,

Thanks but what i wont is little bit complicated,

what i wont is is to do sum for every employee,

in your Example it make sum for all employees.

Regards

i try with this code but i dont success.

DATA: sum_sick_days TYPE p decimals 2,
            tmp_sum TYPE p decimals 2,

  LOOP AT e_tab INTO  wa_e_tab.
    at end of penrnr.
      SUM.
      sum_sick_days = wa_e_tab-sick_days.
      tmp_sum =  sum_sick_days.
    ENDAT.
    wa_e_tab-sum = tmp_sum.
    MODIFY  e_tab FROM wa_e_tab TRANSPORTING sum  .
  ENDLOOP.

Read only

Former Member
0 Likes
1,652

hi ....

i think i misunderstood the qstn earlier,

do like this....

select sum (Sick_days) from itab into sum group by pernr.

write 😕 itab-col1, itab-col2... sum.

endselect.

this will surely help.

do reward if it does.

Read only

0 Likes
1,652

Hi,

i cant do select from internal table.

Regards

Read only

Former Member
0 Likes
1,652

hi....

ok, then lets try like this...

use two loops on the internal table.

loop at itab into wa1.

loop at itab into wa2.

if wa1-pernr eq wa2-pernr.

sum = sum + sick_days.

write : wa2-col1, wa2-col2...., sum..

else.

sum=0.

endif.

endloop.

endloop.

trty this one out and lemme know if it works.

wa1 and wa2 are two workareas for the same internal table.

do reward if this helps or get back again.

Read only

Former Member
0 Likes
1,652

Hi,you can do this in the following way,



Loop at Itab into wa.

at new pernr.

sum = 0.

endat.

sum  = sum + sick_days.

at end pernr.

sum  = sum + sick_days. " Keep and remove and check for the result,you may need it or you may not,Iam not sure

write : wa-pernr,wa-orgeh,wa-sick_days,wa-sum.

endloop.

I think this will work perfectly,for every new pernr sum=0(At new) and for all the iteraions for the same pernr sum will be added with sick days an at the end of the same pernr it will be printed.I commented a line,check for that line.

Reward If Found Useful

Read only

0 Likes
1,652

Hi Rock,

i try your code and it bring the same data like my code:

the problem that in at end of pernr not acsses to the code when we have last emplyeee what i get :

pernr                 period     miss_days        sum
00010153	         200610	     21.80	           21.80
00010155	        200606	     20.56	           20.56
00010158	        200608	    23.00	           23.00
00010158	        200610	    21.56	            21.56
00010158	        200611	    22.00	           22.00



this is my code :

  LOOP AT e_tab INTO wa_e_tab.

    AT NEW penrnr.
      sum = 0.
    ENDAT.
    sum  = sum + wa_e_tab-sick_days.
    AT END OF penrnr.
      sum  = sum + wa_e_tab-sick_days.
      MOVE sum TO  wa_e_tab-sum.
      MODIFY  e_tab FROM wa_e_tab TRANSPORTING sum  .
    ENDAT.
  ENDLOOP.

Regards

Read only

Former Member
0 Likes
1,652

hi ricardo,

the best possible way is the one i hav suggested above using two similar workarea and using two loops.

try that out and lemme know if it helps.

Read only

0 Likes
1,652

Hi perm,

i try like your code and its almost o.k.

i need like in the e.g to put 66 for every line of sum for employee 10158.

pernr      perid       sick_days      sum
10153	200610	  21.80	            21.80
10155	200606	  20.56	           20.56
10158	200608	  23.00	           22.00
10158	200610	  21.56	           44.00
10158	200611	  22.00	           66.00

Regards

Read only

Former Member
0 Likes
1,652

hi there....

thanks...

for that, put the write statement, outside the endif. it will work as required.

if is does, close the qstn by rewarding.

Tc

Read only

Former Member
0 Likes
1,652

the code will look something like this

loop at itab into wa1.

loop at itab into wa2.

if wa1-pernr eq wa2-pernr.

sum = sum + sick_days.

else.

sum=0.

endif.

write : wa2-col1, wa2-col2...., sum..

endloop.

endloop.

Read only

0 Likes
1,652

hi Prem,

i try like your E.g and i have exactly the same output like below.

Regards

LOOP AT e_tab INTO wa_e_tab.
    LOOP AT e_tab INTO wa_e_tab2.
      IF wa_e_tab-penrnr EQ wa_e_tab2-penrnr.
        sum = sum + wa_e_tab-sick_days.
        MOVE sum TO  wa_e_tab-sum.
      ELSE.
        sum =  0.
      ENDIF.
      MODIFY  e_tab FROM wa_e_tab TRANSPORTING sum  .
    ENDLOOP.
  ENDLOOP.

Read only

Former Member
0 Likes
1,652

hi there...

so is the problem solved or still some trouble??

Read only

0 Likes
1,652

Hi,

there is still problem i don't get like in e.g 66 for all employee.

Regards

Read only

Former Member
0 Likes
1,653

hi ricardo...

ur modify statement is not correct.

LOOP AT e_tab INTO wa_e_tab.

LOOP AT e_tab INTO wa_e_tab2.

IF wa_e_tab-penrnr EQ wa_e_tab2-penrnr.

sum = sum + wa_e_tab-sick_days.

MOVE sum TO wa_e_tab-sum.

ELSE.

sum = 0.

ENDIF.

MODIFY e_tab FROM wa_e_tab TRANSPORTING sum .

ENDLOOP.

ENDLOOP.

instead of wa_e_tab use wa_e_tab2.... this is because ur still in the internal loop. so only this wa will affect the rows with same penrnr. use a where condition in this also. like

MODIFY e_tab FROM wa_e_tab2 TRANSPORTING sum where wa_e_tab-penrnr EQ wa_e_tab2-penrnr

i hope this helps.