‎2006 Aug 11 12:50 AM
Hi,
I am not seeing this and can't seem to get this logic to work. I think my brain is wired so badly that I can't think straight for this simple processing.
I am trying to compare one table from another and add the value of one table to another if a field value cost center, for example, matched and create a new record if the field values cost center from both tables don't match.
Here is my code:
SELECT *
INTO CORRESPONDING FIELDS OF TABLE itab_zzzzz1
FROM ztable1.
SELECT *
INTO CORRESPONDING FIELDS OF TABLE itab_yyyyy1
FROM ztable2.
sort: ztable1 by costcenter,
ztable2 by costcenter.
LOOP AT itab_zzzzz1.
READ TABLE itab_yyyyy1 WITH KEY costcenter = itab_zzzzz1-costcenter
BINARY SEARCH.
IF sy-subrc = 0.
MOVE-CORRESPONDING itab_yyyyy1 TO itab_new_value.
MOVE-CORRESPONDING itab_zzzzz1 TO itab_new_value.
itab_new_value-newvalue = itab_zzzzz1-valuez +
itab_yyyyy1-valuey.
ELSE.
MOVE-CORRESPONDING itab_yyyyy1 TO itab_new_value.
MOVE-CORRESPONDING itab_zzzzz1 TO itab_new_value.
itab_new_value-newvalue = itab_yyyyy1-valuey.
ENDIF.
APPEND itab_new_value.
ENDLOOP.
Here is the data:
<b>ztable1</b>
3860610000 26,662.44
3860620003 126,295.78
3860810000 53,992.89
3860860001 100,689.44
3860860002 124,037.72
<b>ztable2</b>
3860137001 50,000.00
3860138000 40,000.00
<b>My logic produces the following:</b>
3860610000 26,662.44
3860620003 126,295.78
3860810000 53,992.89
3860860001 100,689.44
3860860002 124,037.72
<b>Instead of</b>
3860610000 26,662.44
3860620003 126,295.78
3860810000 53,992.89
3860860001 100,689.44
3860860002 124,037.72
3860137001 50,000.00
3860138000 40,000.00
Would someone recommend be best approach to code this?
Thanks,
RT
Message was edited by: Rob Thomas
‎2006 Aug 11 1:19 AM
Since the records in table 2 are never actually being read, this is why they do not end up in the final table. You must read that internal table as well. But this time, you need to check that they don't exist in the first table, because if they do, then they have already been written to the final table.
LOOP AT itab_zzzzz1.
READ TABLE itab_yyyyy1 WITH KEY costcenter = itab_zzzzz1-costcenter
BINARY SEARCH.
IF sy-subrc = 0.
MOVE-CORRESPONDING itab_yyyyy1 TO itab_new_value.
MOVE-CORRESPONDING itab_zzzzz1 TO itab_new_value.
itab_new_value-newvalue = itab_zzzzz1-valuez +
itab_yyyyy1-valuey.
ELSE.
MOVE-CORRESPONDING itab_yyyyy1 TO itab_new_value.
MOVE-CORRESPONDING itab_zzzzz1 TO itab_new_value.
itab_new_value-newvalue = itab_yyyyy1-valuey.
ENDIF.
APPEND itab_new_value.
ENDLOOP.
* Here we need to process the itab_yyyy1 and check
* for the records that were not added in the first
* loop.
<b>LOOP AT itab_yyyy1.
READ TABLE itab_zzzz1 WITH KEY costcenter = itab_yyyy1-costcenter
BINARY SEARCH.
IF sy-subrc <> 0.
MOVE-CORRESPONDING itab_yyyyy1 TO itab_new_value.
itab_new_value-newvalue = itab_yyyyy1-valuey.
APPEND itab_new_value.
ENDIF.
ENDLOOP.</b>
Regards,
Rich Heilman
‎2006 Aug 11 1:19 AM
Since the records in table 2 are never actually being read, this is why they do not end up in the final table. You must read that internal table as well. But this time, you need to check that they don't exist in the first table, because if they do, then they have already been written to the final table.
LOOP AT itab_zzzzz1.
READ TABLE itab_yyyyy1 WITH KEY costcenter = itab_zzzzz1-costcenter
BINARY SEARCH.
IF sy-subrc = 0.
MOVE-CORRESPONDING itab_yyyyy1 TO itab_new_value.
MOVE-CORRESPONDING itab_zzzzz1 TO itab_new_value.
itab_new_value-newvalue = itab_zzzzz1-valuez +
itab_yyyyy1-valuey.
ELSE.
MOVE-CORRESPONDING itab_yyyyy1 TO itab_new_value.
MOVE-CORRESPONDING itab_zzzzz1 TO itab_new_value.
itab_new_value-newvalue = itab_yyyyy1-valuey.
ENDIF.
APPEND itab_new_value.
ENDLOOP.
* Here we need to process the itab_yyyy1 and check
* for the records that were not added in the first
* loop.
<b>LOOP AT itab_yyyy1.
READ TABLE itab_zzzz1 WITH KEY costcenter = itab_yyyy1-costcenter
BINARY SEARCH.
IF sy-subrc <> 0.
MOVE-CORRESPONDING itab_yyyyy1 TO itab_new_value.
itab_new_value-newvalue = itab_yyyyy1-valuey.
APPEND itab_new_value.
ENDIF.
ENDLOOP.</b>
Regards,
Rich Heilman
‎2006 Aug 11 7:34 PM
Hi Rich,
Thank you for the valuable code and for unwiring my brain to think straight. It works like charm. Now I have to give this result for review by the IRON HAND. I am sure my brain will get wired again. So long for now.....
Regards,
RT