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

itab calculations

Former Member
0 Likes
1,080

hello abap masters,

in my requirement,

there are three fields in an internal table

data: begin of itab1,

field1(3) type c,

field2(3) type c,

field3 type p.

data:end of itab1.

now i should calculate total of all (only) field3 's for each fied1,field2 combination and store them in itab2 table, which has only two fields from itab1, ie. field2,field3.

can some one help.

thank you.

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
1,060

Hi

SORT ITAB1 BY FIELD1 FIELD2 FIELD3.

LOOP AT ITAB1.

AT END OF FIELD2.

SUM.

READ TABLE ITAB1 INDEX SY-TABIX.

MOVE-CORRESPONDING ITAB1 TO ITAB2.

APPEND ITAB2.

ENDAT.

CLEAR ITAB2.

ENDLOOP.

Now Itab2 will have your required sums and fields.

Reward points if useful

Regards

Anji

hello abap masters,

in my requirement,

there are three fields in an internal table

data: begin of itab1,

field1(3) type c,

field2(3) type c,

field3 type p.

data:end of itab1.

now i should calculate total of all (only) field3 's for each fied1,field2 combination and store them in itab2 table, which has only two fields from itab1, ie. field2,field3.

can some one help.

thank you.

8 REPLIES 8
Read only

santhosh_patil
Contributor
0 Likes
1,060

Hi,

try this

sort itab1 by field1 field2.

loop at itab1

itab2-field1 = itab2-field1 + itab1-field1.

itab2-field2 = itab2-field2 + itab1-field2

at new field2

append itab2.

endat

endloop

---Patil

Read only

Former Member
0 Likes
1,060

Hi Saritha,

Create another table itab3 of type itab1 and then use below code.

loop at itab1.

collect itab1 to itab3.

endloop.

loop at itab3.

itab2-field2 = itab3-field2.

itab2-field3 = itab3-field3.

append itab2.

endloop.

Reward points if useful.

Regards,

Atish

Read only

Former Member
0 Likes
1,061

Hi

SORT ITAB1 BY FIELD1 FIELD2 FIELD3.

LOOP AT ITAB1.

AT END OF FIELD2.

SUM.

READ TABLE ITAB1 INDEX SY-TABIX.

MOVE-CORRESPONDING ITAB1 TO ITAB2.

APPEND ITAB2.

ENDAT.

CLEAR ITAB2.

ENDLOOP.

Now Itab2 will have your required sums and fields.

Reward points if useful

Regards

Anji

Read only

Former Member
0 Likes
1,060

HI Saritha,

try this logic..

SORT ITAB1 BY FIELD1 FIELD2 FIELD3.

LOOP AT ITAB1.

AT END OF FIELD2.

SUM.

READ TABLE ITAB1 INDEX SY-TABIX.

ITAB2-FIELD2 = ITAB1-FIELD2.

IATB2-FIELD3 = IATB1-FIELD3.

APPEND ITAB2.

ENDAT.

CLEAR ITAB2.

ENDLOOP.

OR

SORT ITAB1 BY FIELD1 FIELD2 FIELD3.

LOOP AT ITAB1.

AT NEW FIELD2.

READ TABLE ITAB1 INDEX SY-TABIX.

ITAB2-FIELD2 = ITAB1-FIELD2.

LOOP AT ITAB1 WHERE FIELD2 = ITAB2-FIELD2.

ITAB2-FIELD3 = ITAB2-FIELD3 + ITAB1-FIELD3.

ENDLOOP.

APPEND ITAB2.

ENDAT.

CLEAR ITAB2.

ENDLOOP.

Regards

SAB

Read only

Former Member
0 Likes
1,060

Hi

sort itab1 by f1 f2 f3.

loop at itab1

at end of f3.

sum.

itab3-f2 = itab2-f2.

itab3-f3 = itab2-f3

append itab2.

endat.

clear itab2.

endloop.

Reward All Helpfull Answers........

Read only

Former Member
0 Likes
1,060

DATA : WTAB1 LIKE ITAB1.

SORT ITAB1 BY F1 F2.

LOOP AT ITAB1.

ITAB2-F2 = ITAB1-F2.

AT END OF F2.

SUM.

ITAB2-F3 = ITAB1-F3.

APPEND ITAB2.

END AT.

ENDLOOP.

REGARDS

SHIBA DUTTA

Read only

Former Member
0 Likes
1,060

Hi Cahnge the code..Not at end of f3..Its at end of f2....

Read only

Former Member
0 Likes
1,060

hi saritha,

try like this,

data:itab2 like itab1 occurs 0 with header line.

sort itab by fld1, fld2.

loop at itab1.

itab2-fld1 = itab1-fld1 + itab1-fld2 + itab1-fld3.

itab2-fld2 = itab1-fld1.

append itab2.

endloop.

if helpful reward some points.

with regards,

suresh babu aluri.