<?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 can you use max with streamstats? in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/How-can-you-use-max-with-streamstats/m-p/263426#M79092</link>
    <description>&lt;P&gt;Try this&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;.... | streamstats window=2 range(Watermark) as ActivityCount by Customer | stats max(ActivityCount) as MaxCount by Customer
&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Mon, 18 Jul 2016 12:36:21 GMT</pubDate>
    <dc:creator>sundareshr</dc:creator>
    <dc:date>2016-07-18T12:36:21Z</dc:date>
    <item>
      <title>How can you use max with streamstats?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-can-you-use-max-with-streamstats/m-p/263425#M79091</link>
      <description>&lt;P&gt;I have a list of events which are watermarks for customer activities.  The data look like this:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;Date/Time      Customer  Watermark
07/12/16 09:00 CustomerA      1000
07/12/16 10:00 CustomerA      5000
07/12/16 11:00 CustomerA      8000
07/12/16 18:00 CustomerB     10000
07/12/16 19:00 CustomerB     12000
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;I need to get the max activity per hour for each customer, so my result set looks like this:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;Customer   Act/Hour
CustomerA      4000
CustomerB      2000
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;I used &lt;STRONG&gt;streamstats&lt;/STRONG&gt; to get the difference and calculate the activities, but when I try and use &lt;STRONG&gt;stats&lt;/STRONG&gt; to get the max of those, streamstats reprocesses the events and messes up the result set.  Here’s my code:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| streamstats current=f window=2 last(watermark) as last_watermark by customer
| eval ActivityCount = watermark - last_watermark
| stats max(ActivityCount) as MaxCount by customer
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;I tried &lt;STRONG&gt;dedup&lt;/STRONG&gt; without any luck either.  I even tried to &lt;STRONG&gt;join&lt;/STRONG&gt; the query to itself, like so:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| join customer_dbname [search index=mysql
  | streamstats current=f window=2 last(watermark) as last_watermark by customer
  | eval ActivityCount = watermark - last_watermark
  | fields customer ActivityCount]
| stats max(ActivityCount) as MaxCount by customer
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;I would have thought the &lt;STRONG&gt;usetime&lt;/STRONG&gt; and &lt;STRONG&gt;earlier&lt;/STRONG&gt; options would have made this work, but no luck here either - ActivityCount came back as blank.  &lt;/P&gt;

&lt;P&gt;Since the customer timing is variable, I need to run this over a large range or else I’ll miss customers.&lt;/P&gt;

&lt;P&gt;Any thoughts/suggestions?  Thanks!&lt;/P&gt;</description>
      <pubDate>Mon, 18 Jul 2016 01:47:29 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-can-you-use-max-with-streamstats/m-p/263425#M79091</guid>
      <dc:creator>Kenshiro70</dc:creator>
      <dc:date>2016-07-18T01:47:29Z</dc:date>
    </item>
    <item>
      <title>Re: How can you use max with streamstats?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-can-you-use-max-with-streamstats/m-p/263426#M79092</link>
      <description>&lt;P&gt;Try this&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;.... | streamstats window=2 range(Watermark) as ActivityCount by Customer | stats max(ActivityCount) as MaxCount by Customer
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 18 Jul 2016 12:36:21 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-can-you-use-max-with-streamstats/m-p/263426#M79092</guid>
      <dc:creator>sundareshr</dc:creator>
      <dc:date>2016-07-18T12:36:21Z</dc:date>
    </item>
    <item>
      <title>Re: How can you use max with streamstats?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-can-you-use-max-with-streamstats/m-p/263427#M79093</link>
      <description>&lt;P&gt;It's much more elegant code, but it still suffers from the same reprocessing problem.  The way I can see this is that if I run the query with earliest=-24h I get fewer results than if I run it with earliest=-8h.  Very weird.&lt;/P&gt;

&lt;P&gt;I may end up just doing several runs with a Date &amp;amp; Time Range of eight hours.&lt;/P&gt;

&lt;P&gt;Thanks for replying.&lt;/P&gt;</description>
      <pubDate>Mon, 18 Jul 2016 16:17:47 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-can-you-use-max-with-streamstats/m-p/263427#M79093</guid>
      <dc:creator>Kenshiro70</dc:creator>
      <dc:date>2016-07-18T16:17:47Z</dc:date>
    </item>
    <item>
      <title>Re: How can you use max with streamstats?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-can-you-use-max-with-streamstats/m-p/263428#M79094</link>
      <description>&lt;P&gt;Give this a try&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;your base search | sort _time  | streamstats current=f window=1 value(watermark) as last_watermark by customer
 | eval ActivityCount = watermark - last_watermark
 | stats max(ActivityCount) as MaxCount by customer
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 18 Jul 2016 16:44:09 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-can-you-use-max-with-streamstats/m-p/263428#M79094</guid>
      <dc:creator>somesoni2</dc:creator>
      <dc:date>2016-07-18T16:44:09Z</dc:date>
    </item>
    <item>
      <title>Re: How can you use max with streamstats?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-can-you-use-max-with-streamstats/m-p/263429#M79095</link>
      <description>&lt;P&gt;The sixteen hour search is still returning more than the eight hour search.  Thanks though.&lt;/P&gt;</description>
      <pubDate>Mon, 18 Jul 2016 18:40:38 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-can-you-use-max-with-streamstats/m-p/263429#M79095</guid>
      <dc:creator>Kenshiro70</dc:creator>
      <dc:date>2016-07-18T18:40:38Z</dc:date>
    </item>
    <item>
      <title>Re: How can you use max with streamstats?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-can-you-use-max-with-streamstats/m-p/263430#M79096</link>
      <description>&lt;P&gt;The answers here spurred me to look further into the raw data, and I realized the results that disappeared when I ran for a longer time range were the ones with multiple values.  That should have been handled by the window setting, but apparently it causes issues.  On a lark I added dedup &lt;STRONG&gt;before&lt;/STRONG&gt; the streamstats, like this:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| dedup 2 customer sortby watermark
| sort customer watermark
| streamstats current=f window=1 values(watermark) as last_watermark by customer
| eval ActivityCount = watermark - last_watermark
| stats max(ActivityCount) as MaxCount by customer | sort -MaxCount
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;This worked in one sense  It gave a much higher number of results when I ran the query for a longer time period.  But when I drilled into the data more, about a third of them weren't showing the true max.  As I feared, putting dedup before the streamstats command just reduced the results to the most recent two.&lt;/P&gt;

&lt;P&gt;Long story short, streamstats and stats just don't play nice.&lt;/P&gt;</description>
      <pubDate>Mon, 18 Jul 2016 19:24:53 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-can-you-use-max-with-streamstats/m-p/263430#M79096</guid>
      <dc:creator>Kenshiro70</dc:creator>
      <dc:date>2016-07-18T19:24:53Z</dc:date>
    </item>
  </channel>
</rss>

