<?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 Summing several fields with SELECT SUM in Application Development and Automation Discussions</title>
    <link>https://community.sap.com/t5/application-development-and-automation-discussions/summing-several-fields-with-select-sum/m-p/4198353#M1003451</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi abappers.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I'm going BONKERS on this silly problem which would take all of about 2 secs to do in something like MySQL.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;The wretched examples with that silly airline application in the SAP help is worse than useless. In all my years of working with SAP I don't think I've EVER found a SINGLE instance of finding a solution to any prioblem I've ever had from the Airline application.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I've got a table which is sorted on Plant (WERKS) and  Material (NATNR)   and some quantity fields fror financial period.&lt;/P&gt;&lt;P&gt;There's sevral records per plant/material.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I want to sum up 2  quantity fields per plant / material.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;This code gives me an error   -  comma without preceding colon  (after SELECT ?).&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;

data: lv_werks   type msku-werks,
      lv_matnr   type mara-matnr,
      lv_sum     type msku-kulab.


loop at t_horizontal into wa_ty_horizontal.
    select werks matnr sum( kulab,kuins )
    into  (lv_werks, lv_matnr, lv_sum )
      from msku
      where matnr eq wa_ty_horizontal-matnr
      and werks eq wa_ty_horizontal-werks
      group by werks matnr.
  endselect.
  * process total value  some other non relevant code here
endloop.

&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;This works but only sums up ONE field -- I want the aggregate of BOTH fields however.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;

loop at t_horizontal into wa_ty_horizontal.
    select werks matnr sum( kulab  )
    into  (lv_werks, lv_matnr, lv_sum )
      from msku
      where matnr eq wa_ty_horizontal-matnr
      and werks eq wa_ty_horizontal-werks
      group by werks matnr.
  endselect.
  * process total value  some other non relevant code here
endloop.

&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;the object is to avoid having to sum the two fields manually&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thanks&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;jimbo.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Thu, 17 Jul 2008 11:38:12 GMT</pubDate>
    <dc:creator>Former Member</dc:creator>
    <dc:date>2008-07-17T11:38:12Z</dc:date>
    <item>
      <title>Summing several fields with SELECT SUM</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/summing-several-fields-with-select-sum/m-p/4198353#M1003451</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi abappers.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I'm going BONKERS on this silly problem which would take all of about 2 secs to do in something like MySQL.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;The wretched examples with that silly airline application in the SAP help is worse than useless. In all my years of working with SAP I don't think I've EVER found a SINGLE instance of finding a solution to any prioblem I've ever had from the Airline application.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I've got a table which is sorted on Plant (WERKS) and  Material (NATNR)   and some quantity fields fror financial period.&lt;/P&gt;&lt;P&gt;There's sevral records per plant/material.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I want to sum up 2  quantity fields per plant / material.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;This code gives me an error   -  comma without preceding colon  (after SELECT ?).&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;

data: lv_werks   type msku-werks,
      lv_matnr   type mara-matnr,
      lv_sum     type msku-kulab.


loop at t_horizontal into wa_ty_horizontal.
    select werks matnr sum( kulab,kuins )
    into  (lv_werks, lv_matnr, lv_sum )
      from msku
      where matnr eq wa_ty_horizontal-matnr
      and werks eq wa_ty_horizontal-werks
      group by werks matnr.
  endselect.
  * process total value  some other non relevant code here
endloop.

&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;This works but only sums up ONE field -- I want the aggregate of BOTH fields however.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;

loop at t_horizontal into wa_ty_horizontal.
    select werks matnr sum( kulab  )
    into  (lv_werks, lv_matnr, lv_sum )
      from msku
      where matnr eq wa_ty_horizontal-matnr
      and werks eq wa_ty_horizontal-werks
      group by werks matnr.
  endselect.
  * process total value  some other non relevant code here
endloop.

&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;the object is to avoid having to sum the two fields manually&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thanks&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;jimbo.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 17 Jul 2008 11:38:12 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/summing-several-fields-with-select-sum/m-p/4198353#M1003451</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2008-07-17T11:38:12Z</dc:date>
    </item>
    <item>
      <title>Re: Summing several fields with SELECT SUM</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/summing-several-fields-with-select-sum/m-p/4198354#M1003452</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hai,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt; Dont get irritated dude.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt; data: lv_werks   type msku-werks,&lt;/P&gt;&lt;P&gt;      lv_matnr   type mara-matnr,&lt;/P&gt;&lt;P&gt;      lv_sum     type msku-kulab.&lt;/P&gt;&lt;P&gt; data:begin of it_sum occurs 0 ,&lt;/P&gt;&lt;P&gt;      lv_werks   type msku-werks,&lt;/P&gt;&lt;P&gt;      lv_matnr   type mara-matnr,&lt;/P&gt;&lt;P&gt;      lv_sum     type msku-kulab,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;        end of it_sum.&lt;/P&gt;&lt;P&gt; &lt;/P&gt;&lt;P&gt;loop at t_horizontal into wa_ty_horizontal.&lt;/P&gt;&lt;P&gt;    select werks matnr kulab kuins &lt;/P&gt;&lt;P&gt;     into  it_sum&lt;/P&gt;&lt;P&gt;     from msku&lt;/P&gt;&lt;P&gt;      where matnr eq wa_ty_horizontal-matnr&lt;/P&gt;&lt;P&gt;      and werks eq wa_ty_horizontal-werks&lt;/P&gt;&lt;P&gt;      group by werks matnr.&lt;/P&gt;&lt;P&gt;collect it_sum .&lt;/P&gt;&lt;P&gt;  endselect.&lt;/P&gt;&lt;P&gt;  &lt;/P&gt;&lt;UL&gt;&lt;LI level="1" type="ul"&gt;&lt;P&gt;process total value  some other non relevant code here&lt;/P&gt;&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;endloop.&lt;/P&gt;&lt;P&gt; &lt;/P&gt;&lt;P&gt;Neeraj&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 17 Jul 2008 11:45:28 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/summing-several-fields-with-select-sum/m-p/4198354#M1003452</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2008-07-17T11:45:28Z</dc:date>
    </item>
    <item>
      <title>Re: Summing several fields with SELECT SUM</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/summing-several-fields-with-select-sum/m-p/4198355#M1003453</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;Try this......&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;data: lv_werks   type msku-werks,&lt;/P&gt;&lt;P&gt;      lv_matnr     type mara-matnr,&lt;/P&gt;&lt;P&gt;      &lt;STRONG&gt;lv_sum_1    type msku-kulab,&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;      &lt;STRONG&gt;lv_sum_2    type msku-kulab.&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt; &lt;/P&gt;&lt;P&gt; &lt;/P&gt;&lt;P&gt;loop at t_horizontal into wa_ty_horizontal.&lt;/P&gt;&lt;P&gt;    &lt;STRONG&gt;select werks matnr sum( kulab ) sum( kuins )&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;    &lt;STRONG&gt;into  (lv_werks, lv_matnr, lv_sum_1,  lv_sum_2 )&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;      from msku&lt;/P&gt;&lt;P&gt;      where matnr eq wa_ty_horizontal-matnr&lt;/P&gt;&lt;P&gt;      and werks eq wa_ty_horizontal-werks&lt;/P&gt;&lt;P&gt;      group by werks matnr.&lt;/P&gt;&lt;P&gt;  endselect.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;  &lt;STRONG&gt;lv_sum_1 = lv_sum_1 + lv_sum_2.&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;  &lt;/P&gt;&lt;P&gt;  &lt;/P&gt;&lt;UL&gt;&lt;LI level="1" type="ul"&gt;&lt;P&gt;process total value  some other non relevant code here&lt;/P&gt;&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;endloop.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Hope this would help you.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Murthy&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 17 Jul 2008 11:49:19 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/summing-several-fields-with-select-sum/m-p/4198355#M1003453</guid>
      <dc:creator>former_member787646</dc:creator>
      <dc:date>2008-07-17T11:49:19Z</dc:date>
    </item>
    <item>
      <title>Re: Summing several fields with SELECT SUM</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/summing-several-fields-with-select-sum/m-p/4198356#M1003454</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Well, if &lt;EM&gt;you&lt;/EM&gt; can't work it out, who could? &lt;SPAN __jive_emoticon_name="wink"&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I can only think of &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;select werks matnr sum( kulab ) sum( kuins )
    into  (lv_werks, lv_matnr, lv_sum_kulab, lv_sum_kuins )&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;but you'd still have to add the two totals...&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thomas&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 17 Jul 2008 11:50:34 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/summing-several-fields-with-select-sum/m-p/4198356#M1003454</guid>
      <dc:creator>ThomasZloch</dc:creator>
      <dc:date>2008-07-17T11:50:34Z</dc:date>
    </item>
    <item>
      <title>Re: Summing several fields with SELECT SUM</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/summing-several-fields-with-select-sum/m-p/4198357#M1003455</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi there&lt;/P&gt;&lt;P&gt;I wanted to avoid the use of Collect -- the aggregate function SUM  should do this with ONE READ per material plant combination. There are roughly 50 lines per materal plant combination.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;For 50,000 materials this would amount to  2,500,000 reads PER PLANT instead of 50,000.&lt;/P&gt;&lt;P&gt;If you have a large number of plants the data base access time increases dramatically - this is why I specifically want to use the AGGREGATE function SUM&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thanks for the other replies -- it seems a shame that you still have to add the total together but at least it's better than the COLLECT solution someone else proposed.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Any self respecting database system should have a SUM multiple fields facility in it.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Cheers&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Jimbo.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 17 Jul 2008 11:56:00 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/summing-several-fields-with-select-sum/m-p/4198357#M1003455</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2008-07-17T11:56:00Z</dc:date>
    </item>
  </channel>
</rss>

