<?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: How to make tstats prestats=true with values() and sum() functions in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/How-to-make-tstats-prestats-true-with-values-and-sum-functions/m-p/586071#M204158</link>
    <description>&lt;P&gt;It might be useful for someone who works on a similar query. I understand why my query returned no data, it all got to do with the field name as it seems rename didn't take effect on the pre-stats fields.&lt;/P&gt;&lt;P&gt;The correct query is the one below.&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;| tstats prestats=true
      values(Traffic.reason),
      sum(Traffic.duration),
      sum(Traffic.sent),
      sum(Traffic.rcvd),
      count AS count
   FROM
      datamodel=Network_Log.Traffic
   BY
      _time span=auto

| stats values(Traffic.reason) AS reason, sum(Traffic.duration) AS duration, sum(Traffic.sent) AS sent, sum(Traffic.rcvd) AS rcvd, count by _time&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 22 Feb 2022 15:57:41 GMT</pubDate>
    <dc:creator>piukr</dc:creator>
    <dc:date>2022-02-22T15:57:41Z</dc:date>
    <item>
      <title>How to make tstats prestats=true with values() and sum() functions?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-make-tstats-prestats-true-with-values-and-sum-functions/m-p/585368#M203939</link>
      <description>&lt;P&gt;I've been using &lt;EM&gt;tstats&lt;/EM&gt; in many queries that I run against accelerated data models, however most of the time I use it with a simple &lt;EM&gt;count()&lt;/EM&gt; function in the following format:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="markup"&gt;| tstats prestats=true count AS count FROM datamodel=... WHERE ... BY ...
| eval ...
| lookup ...
| stats count BY ...&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This time, I will need to add &lt;EM&gt;sum()&lt;/EM&gt; and &lt;EM&gt;values()&lt;/EM&gt; functions to the &lt;EM&gt;tstats&lt;/EM&gt;, but it seems I am unable to get it working.&lt;/P&gt;
&lt;P&gt;If someone could take a look at the queries and let me know what I am doing wrong, that would be great.&lt;/P&gt;
&lt;P&gt;The following query (using &lt;EM&gt;prestats=false&lt;/EM&gt; option) works perfectly and produces output (i.e. the &lt;EM&gt;reason&lt;/EM&gt;,&amp;nbsp; &lt;EM&gt;duration&lt;/EM&gt;, &lt;EM&gt;sent&lt;/EM&gt; and &lt;EM&gt;rcvd&lt;/EM&gt; fields all have correct values).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="markup"&gt;| tstats prestats=false
      values(Traffic.reason),
      sum(Traffic.duration),
      sum(Traffic.sent),
      sum(Traffic.rcvd),
      count AS count
   FROM
      datamodel=Network_Log.Traffic
   BY
      _time span=auto
| rename
     "values(Traffic.reason)" AS reason,
     "sum(Traffic.duration)" AS duration,
     "sum(Traffic.sent)" AS sent,
     "sum(Traffic.rcvd)" AS rcvd&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;When I try to re-write the above query with the &lt;EM&gt;prestats=true&lt;/EM&gt; option and use &lt;EM&gt;stats&lt;/EM&gt; to summarize on the prestats format,&amp;nbsp; the&amp;nbsp;&lt;EM&gt;reason&lt;/EM&gt;,&amp;nbsp; &lt;EM&gt;duration&lt;/EM&gt;, &lt;EM&gt;sent&lt;/EM&gt;, and &lt;EM&gt;rcvd&lt;/EM&gt; fields are all null. The &lt;EM&gt;count&lt;/EM&gt; field is calculated correctly and displayed in the statistics table.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="markup"&gt;| tstats prestats=true
      values(Traffic.reason),
      sum(Traffic.duration),
      sum(Traffic.sent),
      sum(Traffic.rcvd),
      count AS count
   FROM
      datamodel=Network_Log.Traffic
   BY
      _time span=auto
| rename
     "values(Traffic.reason)" AS reason,
     "sum(Traffic.duration)" AS duration,
     "sum(Traffic.sent)" AS sent,
     "sum(Traffic.rcvd)" AS rcvd
| stats values(reason) AS reason, sum(duration) AS duration, sum(sent) AS sent, sum(rcvd) AS rcvd, count by _time&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;By the way, I followed this excellent summary when I started to re-write my queries to &lt;EM&gt;tstats&lt;/EM&gt;, and I think what I tried to do here is in line with the recommendations, i.e. I repeated the same functions in the &lt;EM&gt;stats&amp;nbsp; &lt;/EM&gt;command that I use in &lt;EM&gt;tstats&lt;/EM&gt; and used the same BY clause.&lt;/P&gt;
&lt;P&gt;&lt;A href="https://community.splunk.com/t5/Splunk-Search/What-exactly-are-the-rules-requirements-for-using-quot-tstats/m-p/319801" target="_blank" rel="noopener"&gt;https://community.splunk.com/t5/Splunk-Search/What-exactly-are-the-rules-requirements-for-using-quot-tstats/m-p/319801&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;Regards,&lt;/P&gt;
&lt;P&gt;Robert&lt;/P&gt;</description>
      <pubDate>Tue, 22 Feb 2022 17:19:57 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-make-tstats-prestats-true-with-values-and-sum-functions/m-p/585368#M203939</guid>
      <dc:creator>piukr</dc:creator>
      <dc:date>2022-02-22T17:19:57Z</dc:date>
    </item>
    <item>
      <title>Re: How to make tstats prestats=true with values() and sum() functions</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-make-tstats-prestats-true-with-values-and-sum-functions/m-p/586071#M204158</link>
      <description>&lt;P&gt;It might be useful for someone who works on a similar query. I understand why my query returned no data, it all got to do with the field name as it seems rename didn't take effect on the pre-stats fields.&lt;/P&gt;&lt;P&gt;The correct query is the one below.&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;| tstats prestats=true
      values(Traffic.reason),
      sum(Traffic.duration),
      sum(Traffic.sent),
      sum(Traffic.rcvd),
      count AS count
   FROM
      datamodel=Network_Log.Traffic
   BY
      _time span=auto

| stats values(Traffic.reason) AS reason, sum(Traffic.duration) AS duration, sum(Traffic.sent) AS sent, sum(Traffic.rcvd) AS rcvd, count by _time&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 22 Feb 2022 15:57:41 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-make-tstats-prestats-true-with-values-and-sum-functions/m-p/586071#M204158</guid>
      <dc:creator>piukr</dc:creator>
      <dc:date>2022-02-22T15:57:41Z</dc:date>
    </item>
  </channel>
</rss>

