<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Sum fields from two internal tables in Application Development and Automation Discussions</title>
    <link>https://community.sap.com/t5/application-development-and-automation-discussions/sum-fields-from-two-internal-tables/m-p/414188#M11095</link>
    <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;I think you just need to use COLLECT instruction.&lt;/P&gt;&lt;P&gt;So change your code to something like :&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;* DATA: it_final TYPE STANDARD TABLE OF ty_final, 
DATA : it_final LIKE HASHED TABLE OF ty_final WITH UNIQUE KEY VBELN POSNR,

* Search for every values you have to ADD (in wa_final-NETWR2 and wa_final-FKIMG) and put :
COLLECT wa_final TO it_final.

* Don't forget to CLEAR fields after to prepare next operation
CLEAR : wa_final-FKIMG, wa_final-NETWR2.

* With COLLECT you don't have to double LOOP (LOOP in LOOP) if you don't need to. Just perform 2 LOOPS one after the other. &lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Tue, 04 Jul 2017 15:18:23 GMT</pubDate>
    <dc:creator>bertrand_delvallee</dc:creator>
    <dc:date>2017-07-04T15:18:23Z</dc:date>
    <item>
      <title>Sum fields from two internal tables</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/sum-fields-from-two-internal-tables/m-p/414185#M11092</link>
      <description>&lt;P&gt;&lt;/P&gt;&lt;P&gt;Hi, &lt;/P&gt;&lt;P&gt;I need to sum in an internal table (it_final) the quantity billed from the single positions of the sales order.&lt;/P&gt;&lt;P&gt;I've build two internal tables to extract the relevant sales order positions (ODVAP) and the subsequent billed positions (ODVFATT).&lt;/P&gt;&lt;P&gt;When I try to connect the two internal table into "it_final" the sum doesn't work. How i can fix the problem?&lt;/P&gt;&lt;P&gt;In the infoset, i've inserted in the Coding (used selection criteria DD, DV, etc.):&lt;/P&gt;&lt;P&gt; &lt;/P&gt;&lt;P&gt;1 - DATA&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;DATA: begin of ODVFATT occurs 0,
VBELN like VBAK-VBELN,
POSNR2 like VBAP-POSNR,
ABGRU2 like VBAP-ABGRU,
NETWR2 like VBAP-NETWR,
VBELN3 like VBFA-VBELN,
POSNN3 like VBFA-POSNN,
VBTYP_N3 like VBFA-VBTYP_N,
FKIMG4 like VBRP-FKIMG,
NETWR4 like VBRP-NETWR,
end of ODVFATT,
begin of ODVAP occurs 0,
VBELN like VBAK-VBELN,
POSNR like VBAP-POSNR,
KWMENG like VBAP-KWMENG,
NETWR like VBAP-NETWR,
end of ODVAP.
TYPES: BEGIN OF ty_final, 
VBELN TYPE VBAP-VBELN, 
POSNR TYPE VBAP-POSNR, 
KWMENG TYPE VBAP-KWMENG,
NETWR TYPE VBAP-NETWR, 
FKIMG TYPE VBRP-FKIMG, 
NETWR2 TYPE VBRP-NETWR,
END OF ty_final.
DATA: it_final TYPE STANDARD TABLE OF ty_final, 
wa_final TYPE ty_final,
wa_ODVFATT like ODVFATT, 
wa_ODVAP like ODVAP.&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt; 2 - START-OF-SELECTION&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;select A~VBELN C~POSNR C~ABGRU C~NETWR D~VBELN D~POSNN D~VBTYP_N
F~FKIMG F~NETWR
into table ODVFATT
from VBAK as A inner join VBUK as B
on A~VBELN = B~VBELN
inner join VBAP as C
on A~VBELN = C~VBELN
inner join VBFA as D
on C~VBELN = D~VBELV
and C~POSNR = D~POSNV
inner join VBRK as E
on D~VBELN = E~VBELN
inner join VBRP as F
on D~VBELN = F~VBELN
and D~POSNN = F~POSNR
where A~VBELN IN DV
and A~AUDAT IN DD
and A~ERNAM IN UT
and A~AUART IN TODV
and A~VKORG IN ORG
and A~VTWEG IN CD
and A~KUNNR IN COM
and A~KVGR5 IN AM
and B~GBSTK NE 'A'
and D~VBTYP_N IN ('M', 'P', 'O')
and E~FKSTO = ''.
sort ODVFATT by VBELN POSNR2.
select A~VBELN B~POSNR B~KWMENG B~NETWR
into table ODVAP
from VBAK as A inner join VBAP as B
on A~VBELN = B~VBELN
where A~VBELN IN DV
and A~AUDAT IN DD
and A~ERNAM IN UT
and A~AUART IN TODV
and A~VKORG IN ORG
and A~VTWEG IN CD
and A~KUNNR IN COM
and A~KVGR5 IN AM
and B~ABGRU = ''.
SORT ODVAP BY VBELN POSNR.
LOOP AT ODVAP INTO wa_ODVAP.
READ TABLE ODVFATT INTO wa_ODVFATT WITH KEY VBELN = wa_ODVAP-VBELN 
POSNR2 = wa_ODVAP-POSNR BINARY SEARCH. 
wa_final-VBELN = wa_ODVAP-VBELN. 
wa_final-POSNR = wa_ODVAP-POSNR.
wa_final-KWMENG = wa_ODVAP-KWMENG. 
wa_final-NETWR = wa_ODVAP-NETWR. 
wa_final-FKIMG = wa_final-FKIMG + wa_ODVFATT-FKIMG4. 
wa_final-NETWR2 = wa_final-NETWR2 + wa_ODVFATT-NETWR4. 
APPEND wa_final TO it_final. 
CLEAR: wa_final, wa_ODVFATT, wa_ODVAP. 
ENDLOOP.&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;  &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I've checked the table with an order with the following data.&lt;/P&gt;&lt;P&gt; &lt;A href="https://answers.sap.com/storage/attachments/43643-sum-2-internal-tables.jpg"&gt;sum-2-internal-tables.jpg&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;In my case, I need that the it_final-FKIMG = 18 + 4 not only 18&lt;BR /&gt;and NETWR2 = 519,84 + 115,52 not only 519,84.&lt;/P&gt;&lt;P&gt;Thanks and regards&lt;/P&gt;</description>
      <pubDate>Mon, 03 Jul 2017 14:50:44 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/sum-fields-from-two-internal-tables/m-p/414185#M11092</guid>
      <dc:creator>antonio_bruno</dc:creator>
      <dc:date>2017-07-03T14:50:44Z</dc:date>
    </item>
    <item>
      <title>Re: Sum fields from two internal tables</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/sum-fields-from-two-internal-tables/m-p/414186#M11093</link>
      <description>&lt;P&gt;As a rule, debugging can help ...&lt;/P&gt;&lt;P&gt;Furthermore, check if sy-subrc &amp;lt; &amp;gt; 0 after READ table. Do you really read all the data that you expect and if yes, what happens with them?&lt;/P&gt;</description>
      <pubDate>Mon, 03 Jul 2017 15:18:34 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/sum-fields-from-two-internal-tables/m-p/414186#M11093</guid>
      <dc:creator>retired_member</dc:creator>
      <dc:date>2017-07-03T15:18:34Z</dc:date>
    </item>
    <item>
      <title>Re: Sum fields from two internal tables</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/sum-fields-from-two-internal-tables/m-p/414187#M11094</link>
      <description>&lt;P&gt;Hello Antonio Bruno,&lt;/P&gt;&lt;P&gt;There are a few flaws in your logic, I'll try to explain them.&lt;/P&gt;&lt;P&gt;1. You shouldn't use READ TABLE, but use a LOOP instead.&lt;/P&gt;&lt;P&gt;2. Before you add a new entry to IT_FINAL, check if there isn't already one present. So do a READ TABLE on IT_FINAL and if SY-SUBRC = 0 =&amp;gt; add the quantity fields. If SY-SUBRC &amp;lt;&amp;gt; 0 =&amp;gt; add a new line.&lt;/P&gt;&lt;P&gt;Your coding should look like this:&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;LOOP AT ODVAP INTO wa_ODVAP.
  LOOP AT ODVFATT INTO wa_ODVFATT WHERE VBELN = wa_ODVAP-VBELN 
                                  AND   POSNR2 = wa_ODVAP-POSNR.
  "Check IT_FINAL 
  READ TABLE IT_FINAL ASSIGNING &amp;lt;FS_FINAL&amp;gt; with key VBELN = wa_ODVAP-VBELN 
                                                    POSNR2 = wa_ODVAP-POSNR. 

  IF SY-SUBRC = 0. "=&amp;gt;add quantity/value

  ELSE. "=&amp;gt; add new line to IT_FINAL

  ENDIF.

  ENDLOOP.
ENDLOOP.&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 04 Jul 2017 14:06:57 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/sum-fields-from-two-internal-tables/m-p/414187#M11094</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2017-07-04T14:06:57Z</dc:date>
    </item>
    <item>
      <title>Re: Sum fields from two internal tables</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/sum-fields-from-two-internal-tables/m-p/414188#M11095</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;I think you just need to use COLLECT instruction.&lt;/P&gt;&lt;P&gt;So change your code to something like :&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;* DATA: it_final TYPE STANDARD TABLE OF ty_final, 
DATA : it_final LIKE HASHED TABLE OF ty_final WITH UNIQUE KEY VBELN POSNR,

* Search for every values you have to ADD (in wa_final-NETWR2 and wa_final-FKIMG) and put :
COLLECT wa_final TO it_final.

* Don't forget to CLEAR fields after to prepare next operation
CLEAR : wa_final-FKIMG, wa_final-NETWR2.

* With COLLECT you don't have to double LOOP (LOOP in LOOP) if you don't need to. Just perform 2 LOOPS one after the other. &lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 04 Jul 2017 15:18:23 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/sum-fields-from-two-internal-tables/m-p/414188#M11095</guid>
      <dc:creator>bertrand_delvallee</dc:creator>
      <dc:date>2017-07-04T15:18:23Z</dc:date>
    </item>
  </channel>
</rss>

