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

Caluclate SUM

Former Member
0 Likes
1,382

Hello Gurus,

A text file has data is in the following manner

I have read the file and all properly the only problem

here is summing up the data so below is how my data looks.

Please make a note this is not stored in DDIC database table it will be read in an Internal table

Col1 Col2 Col3

ABC A 1

ABC A 2

ABC B 3

BBC A 4

BBC A 5

I want to Sum up COL3 by grouping COL1 and COL2

so First Record = ABC A 3 ( 1+ 2)

Second Record = ABC B 3

Third Record = BBC A 9 ( 4 + 5 )

Please let me know I tired many logic but nothing seems to be working.. your help is greatly apprecaited .

Points guranteeed !!

Reagrds,

Aryan

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
1,364

Hi,

Do like this

data: wa1 like line of itab,

lv_lines type i,

lv_cnt type i.

describe table itab lines lv_lines.

sort itab by f1 f2.

Loop at itab into wa.

lv_cnt = lv_cnt + 1.

if wa1-f1 = wa-f1 and wa1-f2 = wa-f2.

wa1-f3 = wa1-f3 + wa-f3.

delete itab from wa.

append wa1 to itab.

endif.

if lv_cnt = lv_lines.

exit.

endif.

wa1 = wa.

endloop.

Regards,

Satish

Hi,

Do like this

data: wa1 like line of itab,

lv_lines type i,

lv_cnt type i.

describe table itab lines lv_lines.

sort itab by f1 f2.

Loop at itab into wa.

lv_cnt = lv_cnt + 1.

if wa1-f1 = wa-f1 and wa1-f2 = wa-f2.

wa1-f3 = wa1-f3 + wa-f3.

delete itab from wa.

append wa1 to itab.

endif.

if lv_cnt = lv_lines.

exit.

endif.

wa1 = wa.

endloop.

Regards,

Satish

14 REPLIES 14
Read only

Former Member
0 Likes
1,364

Hi,

try this

loop at itab.

at new col1.

sum.

endat.

endloop

Reward if useful

Regards

Read only

Former Member
0 Likes
1,364

u can achieve this by using control break statments.

Madhavi

Read only

Former Member
0 Likes
1,364

Hi Aryan

Try this code, say you ITAB has the rq. data

-


Sort itab by COL1 COL2.

Loop at itab.

l_value = l_value + itab-COL3.

at end of COL2.

itab1-COL1 = itab-COL1.

itab1-COL2 = itab-COL2.

itab1-COL3 = L_VALUE.

Clear l_value.

endat.

Endloop.

    • Now you ITAB1 will have the data in the req. format.

-


~ Ranganath

Edited by: Ranganath Ramesh on Jan 11, 2008 8:15 AM

Read only

0 Likes
1,364

its on change of col1 and col2 as well its not only

col1 , Please guys try this and reply to me and please paste the abap code if possible really looking forward to your help guys , please create such data in your Internal table

and try calculating the sum , please paste the entire Abap code if it works for you I will award points there itself

Read only

Former Member
0 Likes
1,364

Please guys try this and reply to me and please paste the abap code if possible really looking forward to your help guys , please create such data in your Internal table

and try calculating the sum , please paste the entire Abap code if it works for you I will award points there itself

Read only

Former Member
0 Likes
1,364

Hi,

Read the file in an internal table with 3 cols and then loop at that internal table and claculate the sum based on condition you want to.

regards,

pankaj

Read only

Former Member
0 Likes
1,364

Hello Aryan,

Please use collect on your internal table calculation.

Please reward points if useful.

Regards,

Paresh.

Read only

0 Likes
1,364

Cannot use collect becuase its char field in the text file

Please guys try this and reply to me and please paste the abap code if possible really looking forward to your help guys , please create such data in your Internal table

and try calculating the sum , please paste the entire Abap code if it works for you I will award points there itself

Read only

Former Member
0 Likes
1,364

Hi Aryan,

Try the following code.

Suppose the following data is stored in itab.

Col1 Col2 Col3

ABC A 1

ABC A 2

ABC B 3

BBC A 4

BBC A 5

Declare one more internal table which should have same structure of itab.

Ex.

data: itab1 like itab occurs 0 with header line.

Now

Loop at itab.

move-corresponding itab to itab1.

collect itab1.

clear itab.

endloop.

Now in ur ITAB1, u can see ur expected result.

First Record = ABC A 3 ( 1+ 2)

Second Record = ABC B 3

Third Record = BBC A 9 ( 4 + 5 )

If it is usefull pls do reward pts.

Regards

Srimanta

Read only

Former Member
0 Likes
1,365

Hi,

Do like this

data: wa1 like line of itab,

lv_lines type i,

lv_cnt type i.

describe table itab lines lv_lines.

sort itab by f1 f2.

Loop at itab into wa.

lv_cnt = lv_cnt + 1.

if wa1-f1 = wa-f1 and wa1-f2 = wa-f2.

wa1-f3 = wa1-f3 + wa-f3.

delete itab from wa.

append wa1 to itab.

endif.

if lv_cnt = lv_lines.

exit.

endif.

wa1 = wa.

endloop.

Regards,

Satish

Read only

0 Likes
1,364

Satish , you are some what close , I cannot delete the lines as I need to to have all the lines in Itab

and I am actullay moidfyin an existing abap code

so nothing mich to play around they have used

as Occurs 0 when declaring Itab so I am not using

wa, could you please arrange the data @ your

end and try executing the code

Edited by: Aryan T on Jan 12, 2008 5:37 AM

Read only

Former Member
0 Likes
1,364

Hi there,

You can use COLLECT instead of APPEND.

Full code:

REPORT ZK_TEST3.

types : begin of ty_itab,

col1(3) type c,

col2(1) type c,

col3 type i,

end of ty_itab.

data : gt_itab type table of ty_itab,

wa type ty_itab.

wa-col1 = 'ABC'.

wa-col2 = 'A'.

wa-col3 = '1'.

collect wa into gt_itab.

wa-col1 = 'ABC'.

wa-col2 = 'A'.

wa-col3 = '2'.

collect wa into gt_itab.

wa-col1 = 'ABC'.

wa-col2 = 'B'.

wa-col3 = '3'.

collect wa into gt_itab.

wa-col1 = 'BBC'.

wa-col2 = 'A'.

wa-col3 = '4'.

collect wa into gt_itab.

wa-col1 = 'BBC'.

wa-col2 = 'A'.

wa-col3 = '5'.

collect wa into gt_itab.

loop at gt_itab into wa.

write 😕 wa-col1, wa-col2, wa-col3.

endloop.

Read only

0 Likes
1,364

Hey K. Tiwari make Col3 as Char and try using collect this is what my problem is

Read only

Former Member
0 Likes
1,364

Hi Aryan,

Try this..

data: total type netpr.

loop at itab.

at new.

endat.

total = total + col3.

at end of col1.

endat.

at end of col2.

write:/ total.

clear : total.

endat.

endloop.

Award points if helpful.

Kiran Kumar.G.A

Have a Nice Day..