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: 

To find sum in an internal table - Bit urgent

Former Member
0 Kudos

Hi SAP experts,

I have an internal table with the following fields and data :

Emp_number Section Days

1 A 10

1 B 20

3 A 20

3 B 10

2 A 20

2 B 10

Now I want to calculate the total days for each employee which is a sum of days of section A and days of section B.

I want the data as below into another internal table

:

Emp_number Total_days

1 30

2 30

3 30

Could any one tell the code for the same.

Useful answers wud be rewarded.

Vishnu.

1 ACCEPTED SOLUTION

Former Member
0 Kudos

COLLECT the data into a separate internal table.

Rob

8 REPLIES 8

Former Member
0 Kudos

COLLECT the data into a separate internal table.

Rob

0 Kudos

Hi Rob,

Could you please give the code for the same.

Vishnu

0 Kudos

Vishnu - the others have supplied the code.

Rob

raja_thangamani
Active Contributor
0 Kudos

Here you go:

<div class="jive-quote">

BEGIN OF IT_SUM

selction TYPE C,

Days TYPE I, " << should be I, P or F to get sum for that field

END OF IT_SUM.

LOOP AT ITAB.

MOVE-CORESSPONDING ITAB TO IT_SUM.

COLLECT IT_SUM.

CLEAR IT_SUM.

ENDLOOP.

</div>

Raja T

mnicolai_77
Active Participant
0 Kudos

hi,

emp_tab1 is Emp_number Section Days .

emp_tab2 is Emp_number Total_days.

field-symbols <fs_emp> like emp_tab2.

loop at emp_tab1.

read table emp_tab2 assigning <fs_emp> where Emp_number = emp_tab1-Emp_number.

if sy-subrc = 0.

add emp_tab1-Days to <fs_emp>-total_days.

else.

move emp_tab1-Emp_number to emp_tab2-Emp_number.

move emp_tab1-days to emp_tab2-total_days.

append emp_tab2.

clear emp_tab2.

endif.

endloop.

Former Member
0 Kudos

DATA : begin of itab2 occurs 0,  "new internal table
 emp_number like itab-emp_number,
days like itab-days,
end of itab2.

"your code goes here.

loop at itab.  
itab2-emp_number = itab-emp_number.
itab2-days = itab-days.
COLLECT itab2.
endloop.

raja_thangamani
Active Contributor
0 Kudos

Reply/Reward/close the thread if your issue is resolved.

Raja T

Former Member
0 Kudos

Hi Vishwanath,

In this case you have to use CONTROL BREAK STATEMENTS.

1.At first

2.At new

3.At end

4.At last

1.At first is going to trigger at the 1st loop of the internal table.From the 2nd loop it is not going to trigger.

It is mainly used for sub headings.

2.At new is going to trigger at the new value of the internal table.

3. At end is going to trigger at the end of the new value of a particular field.

It is mainly use for subtotals.

4.At last is going to trigger at the last loop of the internal table.

It is mainly use for grand totals.

So you gothrough the above 4 statements.

In your case you have to use the third statement i.e., At end.

At end

-


ur logic

-


endat.