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

problem in internal table calculation

Former Member
0 Likes
814

i have internal table values as follows ( for eg.)

person month amount1 amount2

A jan rs.500(1) rs.1000 (3)

A feb rs.500 (2) rs.1000

B jan rs.1000(1) rs.2000(4)

B feb rs.1500(2) rs.2000

B mar rs.1000(3) rs.1000

i need to total the values - total = 123+4 and populate to another table as

person amount

A 123 so the value will be 2000

B 123+4 the value will be 5500.

i have written the code using AT NEW AND AT END OF... but i need to work it with IF condition... so plz help me in this regard...

<removed_by_moderator>

thank you

Edited by: Julius Bussche on Sep 5, 2008 7:55 PM

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
770

Hi,

Use the logic.

person month amount1 amount2

A jan rs.500(1) rs.1000 (3)

A feb rs.500 (2) rs.1000

B jan rs.1000(1) rs.2000(4)

B feb rs.1500(2) rs.2000

B mar rs.1000(3) rs.1000

Sort it on person.

take temporary variables for Person.

g_1 and g_2.

g_var is for the amount.

loop on itab.

MOve itab-peson to g_1.

g_index = sY-tabix + 1.

read itab into wa index g_index.

if sy-subrc eq 0 and wa-person = itab-person.

g_var = amount 1 + amount2 + g_var.

elseif sy-subrc eq 0 and wa-person NE itab-person.

append a new record.

clear: g_var.

else.

append a new record with the data

clear: g_var.

endif.

endloop.

Regards,

Venkatesh

6 REPLIES 6
Read only

Former Member
0 Likes
771

Hi,

Use the logic.

person month amount1 amount2

A jan rs.500(1) rs.1000 (3)

A feb rs.500 (2) rs.1000

B jan rs.1000(1) rs.2000(4)

B feb rs.1500(2) rs.2000

B mar rs.1000(3) rs.1000

Sort it on person.

take temporary variables for Person.

g_1 and g_2.

g_var is for the amount.

loop on itab.

MOve itab-peson to g_1.

g_index = sY-tabix + 1.

read itab into wa index g_index.

if sy-subrc eq 0 and wa-person = itab-person.

g_var = amount 1 + amount2 + g_var.

elseif sy-subrc eq 0 and wa-person NE itab-person.

append a new record.

clear: g_var.

else.

append a new record with the data

clear: g_var.

endif.

endloop.

Regards,

Venkatesh

Read only

0 Likes
770

thanks for your immediate response venkatesh.

r u asking me to take g_1 and g_2 for person A and B. here i have given an example showing 2 persons alone. but i need to calculate for 1000's of person. in that case what i should do?

plz guide me...

sri.

Read only

Former Member
0 Likes
770

Hi ,

As i see the requirement,

You need the sum of

total for the amount1 for each person

and amount2 for first month


Loop at itab into wa

at new person.
clear amt1.
v_amt2 = wa-amount2. "ONLY FIRST RECORD
endat.
v_amt1 = wa_amount1 + v_amt1 . "EACH TIME

at end of person.
tot_amount = v_amt1 + v_amt2 .
clear  : v_amt1 ,v_amt2 .

Read only

JozsefSzikszai
Active Contributor
0 Likes
770

hi,

something like this:

SORT itab.
LOOP AT itab INTO wa.
wa_temp = wa. "because of the control break statements
AT FIRST person.
total = total + wa_temp-amount2. "amount2 only needed if first line for person
ENDAT.
total = total + wa-amount1. "amount1 always needed
AT LAST person.
APPEND total TO itab2. "last line of person => data into another internal table
CLEAR total.
ENDAT.
ENDLOOP.

hope this helps

ec

Read only

vinod_vemuru2
Active Contributor
0 Likes
770

Hi Shakthi,

Its very simple. I hope u have the records in internal table.

Just Sort the table by person.

LOOP AT itab INTO wa.

AT END OF person.

SUM.

CLEAR l_sum.

l_sum = wa-amount1 + wa-amount2.

WRITE: /1 wa-person, l_sum.

ENDAT.

ENDLOOP.

Thanks,

Vinod.

Read only

Former Member
0 Likes
770

Hi,

By Sakthi sri

thanks for your immediate response venkatesh.

r u asking me to take g_1 and g_2 for person A and B. here i have given an example showing 2 persons alone. but i need to calculate for 1000's of person. in that case what i should do?

plz guide me...

By Venkatesh

I am sorry to mis guide you with the two variable called G_1 and g_2. there is no use at all for these variables.

What ever may be number of persons in the list. I mean they are not effecting the logic.

for eg Persons from 1 to 100 or 1 to 1000 what ever it is the logic will work.

Regards,

Venkatesh