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

Calculate Sum Tricky one

Former Member
0 Likes
671

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

Also Col3 is not a integer so I cannot use COLLECT

Please suggest a way out to handle this tricky part.

I don;t thik At New On change will work please try this program using the same test data given below , please send me the abap code if you are able to crack this nut.

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

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

Also Col3 is not a integer so I cannot use COLLECT

Please suggest a way out to handle this tricky part.

I don;t thik At New On change will work please try this program using the same test data given below , please send me the abap code if you are able to crack this nut.

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

5 REPLIES 5
Read only

Former Member
0 Likes
628

Hello,

you can try the following:

define a 2nd table itab2 in the same way you've defined itab1

loop at itab1.

read table itab2 with key col1 = itab1-col1

col2 = itab1-col2.

if sy-subrc eq 0.

itab2-col3 = itab2-col3 + itab1-col3.

modify itab2 index sy-tabix.

else.

move: itab1 to itab 2.

append itab2.

endif.

endloop.

But I don't understand, why you not define col3 as a numeric value and use the collect statement?

Best regards

Stephan

Read only

0 Likes
628

Hello Stephan , Thanks much , but I modifying an exsiting program so nothing much to play around have to stick with the existing logic , anyways thanks a lot for helping me out I will award you points then !!

Read only

0 Likes
628

Could you post the existing code then so that we can help you with minimum changes to the existing logic?

Read only

Former Member
0 Likes
628

Hi,

Try it

loop at itab1.

if wa_col1 = itab1-col1 and wa_col2 = itab-col2.

wa_col3 = wa_col3 + itab1-col3.

else.

itab2-col1 = wa_col1.

itab2-col2 = wa_col2.

itab2-col3 = wa_col3.

modify itab2.

wa_col1 = itab1-col1.

wa_col2 = itab1-col2.

wa_col3 = itab1-col3.

endif.

endloop.

wa_col3 is number type.You will get result in table itab2.

L.Velu

Read only

Former Member
0 Likes
628

Hi Aryan,

I was able to do the sum... check the code below...hope it helps..

TYPES: BEGIN OF TY_ITAB,

COL1(3) TYPE C,

COL2 TYPE C,

COL3(2) TYPE C,

END OF TY_ITAB.

DATA: ITAB TYPE TABLE OF TY_ITAB,

IT_FINAL TYPE TABLE OF TY_ITAB WITH HEADER LINE,

WA TYPE TY_ITAB,

WA1 TYPE TY_ITAB,

COUNT TYPE I,

SUM TYPE I,

FLAG TYPE FLAG.

WA-COL1 = 'ABC'.

WA-COL2 = 'A'.

WA-COL3 = '1'.

APPEND WA TO ITAB.

WA-COL1 = 'ABC'.

WA-COL2 = 'A'.

WA-COL3 = '1'.

APPEND WA TO ITAB.

WA-COL1 = 'ABC'.

WA-COL2 = 'A'.

WA-COL3 = '2'.

APPEND WA TO ITAB.

WA-COL1 = 'ABC'.

WA-COL2 = 'B'.

WA-COL3 = '23'.

APPEND WA TO ITAB.

WA-COL1 = 'ABC'.

WA-COL2 = 'A'.

WA-COL3 = '10'.

APPEND WA TO ITAB.

WA-COL1 = 'ABC'.

WA-COL2 = 'B'.

WA-COL3 = '12'.

APPEND WA TO ITAB.

WA-COL1 = 'ABC'.

WA-COL2 = 'B'.

WA-COL3 = '9'.

APPEND WA TO ITAB.

WA-COL1 = 'ABC'.

WA-COL2 = 'A'.

WA-COL3 = '2'.

APPEND WA TO ITAB.

SORT ITAB BY COL1 COL2.

LOOP AT ITAB INTO WA.

READ TABLE IT_FINAL WITH KEY COL1 = WA-COL1

COL2 = WA-COL2.

IF SY-SUBRC = 0.

CONTINUE.

ENDIF.

CLEAR: WA1 , SUM ,FLAG.

READ TABLE ITAB INTO WA1 WITH KEY COL1 = WA-COL1

COL2 = WA-COL2

BINARY SEARCH.

IF SY-SUBRC = 0.

COUNT = SY-TABIX + 1.

SUM = WA1-COL3.

CLEAR WA1.

WHILE ( FLAG NE 'X').

READ TABLE ITAB INTO WA1 INDEX COUNT.

IF WA1-COL1 = WA-COL1 AND WA1-COL2 = WA-COL2.

SUM = SUM + WA1-COL3.

COUNT = SY-TABIX + 1.

ELSE.

FLAG = 'X'.

ENDIF.

ENDWHILE.

ENDIF.

IT_FINAL-COL1 = WA-COL1.

IT_FINAL-COL2 = WA-COL2.

IT_FINAL-COL3 = SUM.

APPEND IT_FINAL.

CLEAR IT_FINAL.

ENDLOOP.

LOOP AT IT_FINAL.

WRITE:/ IT_FINAL-COL1 , 10 IT_FINAL-COL2 , 20 IT_FINAL-COL3.

ENDLOOP.