<?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 function in a for loop/Group by in App Development</title>
    <link>https://community.qlik.com/t5/App-Development/Sum-function-in-a-for-loop-Group-by/m-p/1997441#M82610</link>
    <description>&lt;P&gt;Hi, not quite sure why you would want to loop through every shop value, but here's a code that will work for you (might need to adjust it a little bit since I'm using resident):&lt;/P&gt;
&lt;LI-CODE lang="python"&gt;temp:
LOAD *, ROWNO() AS Row INLINE [
ShopCode, Product, Qty
1A, XYZ, 10
1B, ZVX, 20,
1C, TYW, 30
1A, XYZ, 10
1A, XYZ, 30
];

SHOP:
LOAD
FIELDVALUE('ShopCode',RECNO()) AS ShopCode
AUTOGENERATE FIELDVALUECOUNT('ShopCode');

result:
LOAD 0 AS temp_field
AUTOGENERATE 0;

FOR i=0 TO NOOFROWS('SHOP')-1

LET vShop = PEEK('ShopCode',$(i),'SHOP');
TRACE vShop = $(vShop);

CONCATENATE (result)
LOAD
ShopCode,
Product,
SUM(Qty) as Qty
RESIDENT temp
WHERE MATCH(ShopCode,'$(vShop)')
GROUP BY ShopCode,Product;

NEXT;

DROP TABLE temp,SHOP;
DROP FIELD temp_field;&lt;/LI-CODE&gt;</description>
    <pubDate>Thu, 27 Oct 2022 07:49:32 GMT</pubDate>
    <dc:creator>RsQK</dc:creator>
    <dc:date>2022-10-27T07:49:32Z</dc:date>
    <item>
      <title>Sum function in a for loop/Group by</title>
      <link>https://community.qlik.com/t5/App-Development/Sum-function-in-a-for-loop-Group-by/m-p/1997436#M82608</link>
      <description>&lt;P&gt;Hello, I am using for loop and group by function to sum the value of a field if they are the same. The for loop and group by function is working but I've noticed that when the ShopCode and Product has same values but with different value from Qty field, the output will not be summed hence I am getting double entries/record.&lt;/P&gt;
&lt;P&gt;My script is:&lt;/P&gt;
&lt;P&gt;SHOP:&lt;/P&gt;
&lt;P&gt;LOAD DISTINCT&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;ShopCode&lt;BR /&gt;FROM []&lt;BR /&gt;(qvd);&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;For i=0 to NoOfRows('SHOP')-1&lt;/P&gt;
&lt;P&gt;Let vShop = Peek('ShopCode', i , 'SHOP');&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;1_SHOP:&lt;/P&gt;
&lt;P&gt;LOAD&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;ShopCode&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Product&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;sum(Qty) as S_Qty&lt;BR /&gt;FROM []&lt;BR /&gt;(qvd)&lt;BR /&gt;where ShopCode = '$(vShop)' ;&lt;BR /&gt;Group by &lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;ShopCode&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Product&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Qty;&lt;/P&gt;
&lt;P&gt;Store 1_SHOP into [$(vShop).csv] (txt);&lt;BR /&gt;Drop table 1_SHOP;&lt;/P&gt;
&lt;P&gt;Next i&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here is the sample table:&lt;/P&gt;
&lt;P&gt;ShopCode, Product, Qty&lt;/P&gt;
&lt;P&gt;1A, XYZ, 10&lt;/P&gt;
&lt;P&gt;1B, ZVX, 20,&lt;/P&gt;
&lt;P&gt;1C, TYW, 30&lt;/P&gt;
&lt;P&gt;1A, XYZ, 10&lt;/P&gt;
&lt;P&gt;1A, XYZ, 30&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This is the output I'm getting:&lt;/P&gt;
&lt;P&gt;ShopCode, Product, Qty&lt;/P&gt;
&lt;P&gt;1A, XYZ, 20&lt;/P&gt;
&lt;P&gt;1A, XYZ, 30&lt;/P&gt;
&lt;P&gt;1B, ZBX, 20&lt;/P&gt;
&lt;P&gt;1C, TYW, 30&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;double entries for the 1A, XYZ. The value for that field with same Qty is summed but the other one with different value is not added.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The output should be :&lt;/P&gt;
&lt;P&gt;ShopCode, Product, Qty&lt;/P&gt;
&lt;P&gt;1A, XYZ, 50&lt;/P&gt;
&lt;P&gt;1B, ZBX, 20&lt;/P&gt;
&lt;P&gt;1C, TYW, 30&lt;/P&gt;</description>
      <pubDate>Thu, 27 Oct 2022 07:39:05 GMT</pubDate>
      <guid>https://community.qlik.com/t5/App-Development/Sum-function-in-a-for-loop-Group-by/m-p/1997436#M82608</guid>
      <dc:creator>bryan_21</dc:creator>
      <dc:date>2022-10-27T07:39:05Z</dc:date>
    </item>
    <item>
      <title>Re: Sum function in a for loop/Group by</title>
      <link>https://community.qlik.com/t5/App-Development/Sum-function-in-a-for-loop-Group-by/m-p/1997441#M82610</link>
      <description>&lt;P&gt;Hi, not quite sure why you would want to loop through every shop value, but here's a code that will work for you (might need to adjust it a little bit since I'm using resident):&lt;/P&gt;
&lt;LI-CODE lang="python"&gt;temp:
LOAD *, ROWNO() AS Row INLINE [
ShopCode, Product, Qty
1A, XYZ, 10
1B, ZVX, 20,
1C, TYW, 30
1A, XYZ, 10
1A, XYZ, 30
];

SHOP:
LOAD
FIELDVALUE('ShopCode',RECNO()) AS ShopCode
AUTOGENERATE FIELDVALUECOUNT('ShopCode');

result:
LOAD 0 AS temp_field
AUTOGENERATE 0;

FOR i=0 TO NOOFROWS('SHOP')-1

LET vShop = PEEK('ShopCode',$(i),'SHOP');
TRACE vShop = $(vShop);

CONCATENATE (result)
LOAD
ShopCode,
Product,
SUM(Qty) as Qty
RESIDENT temp
WHERE MATCH(ShopCode,'$(vShop)')
GROUP BY ShopCode,Product;

NEXT;

DROP TABLE temp,SHOP;
DROP FIELD temp_field;&lt;/LI-CODE&gt;</description>
      <pubDate>Thu, 27 Oct 2022 07:49:32 GMT</pubDate>
      <guid>https://community.qlik.com/t5/App-Development/Sum-function-in-a-for-loop-Group-by/m-p/1997441#M82610</guid>
      <dc:creator>RsQK</dc:creator>
      <dc:date>2022-10-27T07:49:32Z</dc:date>
    </item>
    <item>
      <title>Re: Sum function in a for loop/Group by</title>
      <link>https://community.qlik.com/t5/App-Development/Sum-function-in-a-for-loop-Group-by/m-p/1997500#M82616</link>
      <description>&lt;P&gt;Thanks! Finally figured it out, modified some though.&lt;/P&gt;</description>
      <pubDate>Thu, 27 Oct 2022 10:08:50 GMT</pubDate>
      <guid>https://community.qlik.com/t5/App-Development/Sum-function-in-a-for-loop-Group-by/m-p/1997500#M82616</guid>
      <dc:creator>bryan_21</dc:creator>
      <dc:date>2022-10-27T10:08:50Z</dc:date>
    </item>
  </channel>
</rss>

