2008 Jan 12 5:03 AM
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
2008 Jan 12 12:59 PM
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
2008 Jan 14 1:32 AM
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 !!
2008 Jan 16 4:12 PM
Could you post the existing code then so that we can help you with minimum changes to the existing logic?
2008 Jan 14 12:53 PM
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
2008 Jan 17 4:52 AM
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.