<?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 in itab in Application Development and Automation Discussions</title>
    <link>https://community.sap.com/t5/application-development-and-automation-discussions/sum-in-itab/m-p/1189508#M128103</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;HI priya,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;1. Is there any other way to do it.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;  If u want only the sum, (not the detail info)&lt;/P&gt;&lt;P&gt; , then this is the best way.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt; (ie., instead of reading all records from database&lt;/P&gt;&lt;P&gt;  ,and then applying sum logic, &lt;/P&gt;&lt;P&gt;  we can STRAIGHT away read the sum &lt;/P&gt;&lt;P&gt;  using the SQL)&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;2. However, if u ALSO want to show&lt;/P&gt;&lt;P&gt;   the detailed records,&lt;/P&gt;&lt;P&gt;  then u need to fetch all records first,&lt;/P&gt;&lt;P&gt;  and then either use &lt;/P&gt;&lt;P&gt;   a) collect&lt;/P&gt;&lt;P&gt;   b) use alv total functionality&lt;/P&gt;&lt;P&gt;   c) something else&lt;/P&gt;&lt;P&gt; &lt;/P&gt;&lt;P&gt; &lt;/P&gt;&lt;P&gt;regards,&lt;/P&gt;&lt;P&gt;amit m.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Fri, 10 Feb 2006 10:59:34 GMT</pubDate>
    <dc:creator>Former Member</dc:creator>
    <dc:date>2006-02-10T10:59:34Z</dc:date>
    <item>
      <title>SUM in itab</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/sum-in-itab/m-p/1189501#M128096</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi all,.&lt;/P&gt;&lt;P&gt;I need to load some data into an internal table.&lt;/P&gt;&lt;P&gt;I have to sum a field and select it.&lt;/P&gt;&lt;P&gt;Presently, I am using the select statement followed by group by clause.&lt;/P&gt;&lt;P&gt;Like ,&lt;/P&gt;&lt;P&gt;select field1&lt;/P&gt;&lt;P&gt;       sum( field2 )&lt;/P&gt;&lt;P&gt;from ....&lt;/P&gt;&lt;P&gt;group by&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Is there any other way to do it.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Regards, &lt;/P&gt;&lt;P&gt;Priya&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 10 Feb 2006 10:48:58 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/sum-in-itab/m-p/1189501#M128096</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2006-02-10T10:48:58Z</dc:date>
    </item>
    <item>
      <title>Re: SUM in itab</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/sum-in-itab/m-p/1189502#M128097</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi priya,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Yes we can do it using ALV's in that use sum option.and one more thing don't use these arthemetic operations and group by b'coz it consumes the systenm resources.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;regards,&lt;/P&gt;&lt;P&gt;&amp;lt;i&amp;gt;&amp;lt;b&amp;gt;Sri&amp;lt;/b&amp;gt;&amp;lt;/i&amp;gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 10 Feb 2006 10:51:58 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/sum-in-itab/m-p/1189502#M128097</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2006-02-10T10:51:58Z</dc:date>
    </item>
    <item>
      <title>Re: SUM in itab</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/sum-in-itab/m-p/1189503#M128098</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;can also use collect statement&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 10 Feb 2006 10:52:23 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/sum-in-itab/m-p/1189503#M128098</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2006-02-10T10:52:23Z</dc:date>
    </item>
    <item>
      <title>Re: SUM in itab</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/sum-in-itab/m-p/1189504#M128099</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;hi,&lt;/P&gt;&lt;P&gt;the way you are doing is correct.&lt;/P&gt;&lt;P&gt;you can also do at itab level by using SUM statement.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Regards,&lt;/P&gt;&lt;P&gt;Abdul Hakim&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 10 Feb 2006 10:52:55 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/sum-in-itab/m-p/1189504#M128099</guid>
      <dc:creator>abdul_hakim</dc:creator>
      <dc:date>2006-02-10T10:52:55Z</dc:date>
    </item>
    <item>
      <title>Re: SUM in itab</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/sum-in-itab/m-p/1189505#M128100</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;HI Priya,&lt;/P&gt;&lt;P&gt;  You can better bring the contents to the internal table and &lt;/P&gt;&lt;P&gt; LOOP AT ITAB.&lt;/P&gt;&lt;P&gt;....&lt;/P&gt;&lt;P&gt;   AT LAST.&lt;/P&gt;&lt;P&gt;    SUM.&lt;/P&gt;&lt;P&gt;   ENDAT.&lt;/P&gt;&lt;P&gt;ENDLOOP.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I think this is better ..&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;regards&lt;/P&gt;&lt;P&gt;satesh&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 10 Feb 2006 10:53:15 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/sum-in-itab/m-p/1189505#M128100</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2006-02-10T10:53:15Z</dc:date>
    </item>
    <item>
      <title>Re: SUM in itab</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/sum-in-itab/m-p/1189506#M128101</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi Priya, &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;    First do normal selection of data using Select Statement. &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;    Sort the Internal Table. &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;    Then use COLLECT statement.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Here the SAP help For u .&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;The following statement allows you to summate entries in an internal table:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;COLLECT &amp;lt;wa&amp;gt; INTO &amp;lt;itab&amp;gt;.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&amp;lt;itab&amp;gt; must have a flat line type, and all of the fields that are not part of the table key must have a numeric type (F, I, or P). You specify the line that you want to add in a work area that is compatible with the line type. &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;When the line is inserted, the system checks whether there is already a table entry that matches the key. If there is no corresponding entry already in the table, the COLLECT statement has the same effect as inserting the new line. If an entry with the same key already exists, the COLLECT statement does not append a new line, but adds the contents of the numeric fields in the work area to the contents of the numeric fields in the existing entry. &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;You should only use the COLLECT statement if you want to create summarized tables. If you use other statements to insert table entries, you may end up with duplicate entries. &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Lines are added to internal tables as follows:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Standard tables&lt;/P&gt;&lt;P&gt;If the COLLECT statement is the first statement to fill the standard table, the system creates a temporary hash administration that identifies existing entries in the table. The hash administration is retained until another statement changes the contents of key fields or changes the sequence of the lines in the internal table. After this, the system finds existing entries using a linear search. The runtime for this operation increases in linear relation to the number of existing table entries. The system field SY-TABIX contains the index of the line inserted or modified in the COLLECT statement.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Sorted tables&lt;/P&gt;&lt;P&gt;The system uses a binary search to locate existing lines. The runtime for the operation increases logarithmically with the number of existing lines. The system field SY-TABIX contains the index of the line inserted or modified in the COLLECT statement.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Hashed tables&lt;/P&gt;&lt;P&gt;The system finds existing lines using the hash algorithm of the internal table. After the COLLECT statement, the system field SY-TABIX has the value 0, since hashed tables have no linear index. &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Example&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;DATA: BEGIN OF LINE,&lt;/P&gt;&lt;P&gt;        COL1(3) TYPE C,&lt;/P&gt;&lt;P&gt;        COL2(2) TYPE N,&lt;/P&gt;&lt;P&gt;        COL3    TYPE I,&lt;/P&gt;&lt;P&gt;      END OF LINE.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;DATA ITAB LIKE SORTED TABLE OF LINE &lt;/P&gt;&lt;P&gt;          WITH NON-UNIQUE KEY COL1 COL2.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;LINE-COL1 = 'abc'. LINE-COL2 = '12'. LINE-COL3 = 3.&lt;/P&gt;&lt;P&gt;COLLECT LINE INTO ITAB.&lt;/P&gt;&lt;P&gt;WRITE / SY-TABIX.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;LINE-COL1 = 'def'. LINE-COL2 = '34'. LINE-COL3 = 5.&lt;/P&gt;&lt;P&gt;COLLECT LINE INTO ITAB.&lt;/P&gt;&lt;P&gt;WRITE / SY-TABIX.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;LINE-COL1 = 'abc'. LINE-COL2 = '12'. LINE-COL3 = 7.&lt;/P&gt;&lt;P&gt;COLLECT LINE INTO ITAB.&lt;/P&gt;&lt;P&gt;WRITE / SY-TABIX.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;LOOP AT ITAB INTO LINE.&lt;/P&gt;&lt;P&gt;  WRITE: / LINE-COL1, LINE-COL2, LINE-COL3.&lt;/P&gt;&lt;P&gt;ENDLOOP.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;The output is: &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;         1&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;         2&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;         1&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;abc 12 10&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;def 34           5&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;The example fills a sorted table. The first two COLLECT statements work like normal insertion statements. In the third COLLECT statement, the first line of ITAB is modified. The following diagram shows the three steps:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thanks, &lt;/P&gt;&lt;P&gt;Srinivas.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 10 Feb 2006 10:54:29 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/sum-in-itab/m-p/1189506#M128101</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2006-02-10T10:54:29Z</dc:date>
    </item>
    <item>
      <title>Re: SUM in itab</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/sum-in-itab/m-p/1189507#M128102</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;HI priya&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;If you want to print the sum in your report then you have to use&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Loop at itab.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;at last.&lt;/P&gt;&lt;P&gt;sum.&lt;/P&gt;&lt;P&gt;write: itab-&amp;lt;fieldname&amp;gt;.&lt;/P&gt;&lt;P&gt;endat.&lt;/P&gt;&lt;P&gt;endloop.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;this will print the sum.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;But if you wnat the sum to be in a variable then you have to use&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;select field1 sum(field2) into &amp;lt;itab&amp;gt; from &amp;lt;table&amp;gt; group by &amp;lt;key field&amp;gt;.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;regards&lt;/P&gt;&lt;P&gt;kishore&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 10 Feb 2006 10:56:59 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/sum-in-itab/m-p/1189507#M128102</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2006-02-10T10:56:59Z</dc:date>
    </item>
    <item>
      <title>Re: SUM in itab</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/sum-in-itab/m-p/1189508#M128103</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;HI priya,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;1. Is there any other way to do it.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;  If u want only the sum, (not the detail info)&lt;/P&gt;&lt;P&gt; , then this is the best way.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt; (ie., instead of reading all records from database&lt;/P&gt;&lt;P&gt;  ,and then applying sum logic, &lt;/P&gt;&lt;P&gt;  we can STRAIGHT away read the sum &lt;/P&gt;&lt;P&gt;  using the SQL)&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;2. However, if u ALSO want to show&lt;/P&gt;&lt;P&gt;   the detailed records,&lt;/P&gt;&lt;P&gt;  then u need to fetch all records first,&lt;/P&gt;&lt;P&gt;  and then either use &lt;/P&gt;&lt;P&gt;   a) collect&lt;/P&gt;&lt;P&gt;   b) use alv total functionality&lt;/P&gt;&lt;P&gt;   c) something else&lt;/P&gt;&lt;P&gt; &lt;/P&gt;&lt;P&gt; &lt;/P&gt;&lt;P&gt;regards,&lt;/P&gt;&lt;P&gt;amit m.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 10 Feb 2006 10:59:34 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/sum-in-itab/m-p/1189508#M128103</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2006-02-10T10:59:34Z</dc:date>
    </item>
    <item>
      <title>Re: SUM in itab</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/sum-in-itab/m-p/1189509#M128104</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;i feel select the records, later using control break statements or collect you can sum them , performance point it is better opttion.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;regards&lt;/P&gt;&lt;P&gt;vijay&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 10 Feb 2006 11:00:44 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/sum-in-itab/m-p/1189509#M128104</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2006-02-10T11:00:44Z</dc:date>
    </item>
  </channel>
</rss>

