<?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 Should I use Sum() or Count(DISTINCT) in QlikView</title>
    <link>https://community.qlik.com/t5/QlikView/Should-I-use-Sum-or-Count-DISTINCT/m-p/314473#M116036</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hello experts,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;According to the QV manuals (and QT's training courses) I should try to avoid using Count(DISTINCT X) and try to add 1 for each row ( e.g. 1 as Counter) in the table and use Sum(Counter). The sum() method is much faster than Count(DISTINCT X).&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I'm changing one of my applications to get the application optimized.&lt;BR /&gt;The first reports I changed from Count(DISTINCT) to Sum() where considerably faster after the change.&lt;BR /&gt;But a lot of the reports are a bit more complicated than just Sum(Counter). I'm frequently using set modifiers to only count data that a specific report needs.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;To my surprise using Count(DISTINCT) is often faster than using sum() when the reports get more complicated.&lt;BR /&gt;More complicated in this case is, in the report, to use the following as an expression:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;If(Aggr(Sum({$ &amp;lt;TypeID={7}&amp;gt;}Head_Counter),OrderNumber) &amp;gt; 1, Sum(Head_Counter)/(Sum({$ &amp;lt;TypeID={7}&amp;gt;}Head_Counter)), (0/1))&lt;/P&gt;&lt;P&gt;Compared to&lt;/P&gt;&lt;P&gt;Count (DISTINCT If(Aggr(Count(DISTINCT InvNo),OrderNumber) &amp;gt; 1, OrderNumber)))&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;In my application I have around 3 million rows and I have created 2 separate report where one uses Sum() and the other Count(DISTINCT).&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I'm aware that the number of rows in the table might change the result but I'm wondering when to use sum() and when to use Count(DISTINCT).&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Should the approach be to do both and use whatever method is fastest? This will take more time in total.&lt;/P&gt;&lt;P&gt;Or should I still use Sum() (perhaps it is the logic in my report that can be optimized)?&lt;BR /&gt;I.e. If(Aggr(Sum({$ &amp;lt;TypeID={7}&amp;gt;}Head_Counter),OrderNumber) &amp;gt; 1, Sum(Head_Counter)/(Sum({$ &amp;lt;TypeID={7}&amp;gt;}Head_Counter)), (0/1))&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Anyone that can enlighten the rest of us or has come accross the same thing and found an effective approcah to this dilemma?&lt;/P&gt;&lt;P&gt;br &lt;BR /&gt;Martin&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Wed, 05 Oct 2011 13:36:22 GMT</pubDate>
    <dc:creator />
    <dc:date>2011-10-05T13:36:22Z</dc:date>
    <item>
      <title>Should I use Sum() or Count(DISTINCT)</title>
      <link>https://community.qlik.com/t5/QlikView/Should-I-use-Sum-or-Count-DISTINCT/m-p/314473#M116036</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hello experts,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;According to the QV manuals (and QT's training courses) I should try to avoid using Count(DISTINCT X) and try to add 1 for each row ( e.g. 1 as Counter) in the table and use Sum(Counter). The sum() method is much faster than Count(DISTINCT X).&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I'm changing one of my applications to get the application optimized.&lt;BR /&gt;The first reports I changed from Count(DISTINCT) to Sum() where considerably faster after the change.&lt;BR /&gt;But a lot of the reports are a bit more complicated than just Sum(Counter). I'm frequently using set modifiers to only count data that a specific report needs.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;To my surprise using Count(DISTINCT) is often faster than using sum() when the reports get more complicated.&lt;BR /&gt;More complicated in this case is, in the report, to use the following as an expression:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;If(Aggr(Sum({$ &amp;lt;TypeID={7}&amp;gt;}Head_Counter),OrderNumber) &amp;gt; 1, Sum(Head_Counter)/(Sum({$ &amp;lt;TypeID={7}&amp;gt;}Head_Counter)), (0/1))&lt;/P&gt;&lt;P&gt;Compared to&lt;/P&gt;&lt;P&gt;Count (DISTINCT If(Aggr(Count(DISTINCT InvNo),OrderNumber) &amp;gt; 1, OrderNumber)))&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;In my application I have around 3 million rows and I have created 2 separate report where one uses Sum() and the other Count(DISTINCT).&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I'm aware that the number of rows in the table might change the result but I'm wondering when to use sum() and when to use Count(DISTINCT).&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Should the approach be to do both and use whatever method is fastest? This will take more time in total.&lt;/P&gt;&lt;P&gt;Or should I still use Sum() (perhaps it is the logic in my report that can be optimized)?&lt;BR /&gt;I.e. If(Aggr(Sum({$ &amp;lt;TypeID={7}&amp;gt;}Head_Counter),OrderNumber) &amp;gt; 1, Sum(Head_Counter)/(Sum({$ &amp;lt;TypeID={7}&amp;gt;}Head_Counter)), (0/1))&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Anyone that can enlighten the rest of us or has come accross the same thing and found an effective approcah to this dilemma?&lt;/P&gt;&lt;P&gt;br &lt;BR /&gt;Martin&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 05 Oct 2011 13:36:22 GMT</pubDate>
      <guid>https://community.qlik.com/t5/QlikView/Should-I-use-Sum-or-Count-DISTINCT/m-p/314473#M116036</guid>
      <dc:creator />
      <dc:date>2011-10-05T13:36:22Z</dc:date>
    </item>
  </channel>
</rss>

