<?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: Time Difference and Average in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/Time-Difference-and-Average/m-p/537422#M151928</link>
    <description>&lt;P&gt;If I use | table it works fine, if I use | stats avg(time_dur) it doesn't show the right values.&lt;/P&gt;</description>
    <pubDate>Wed, 27 Jan 2021 14:00:16 GMT</pubDate>
    <dc:creator>geekf</dc:creator>
    <dc:date>2021-01-27T14:00:16Z</dc:date>
    <item>
      <title>Time Difference and Average</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Time-Difference-and-Average/m-p/537304#M151878</link>
      <description>&lt;P&gt;I am trying to get the average of a time difference by using&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;| stats avg(time_dur) by type&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;and since I am using this search&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;| eval time_dur=tostring(strptime(LastSeen,"%d/%m/%y %H:%M:%S")-strptime(FirstSeen,"%d/%m/%y %H:%M:%S"),"duration")&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I think the data is coming in a string, and because of that, I am not getting any results. If I use&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;eval time_dur=(strptime(LastSeen,"%d/%m/%y %H:%M:%S")-strptime(FirstSeen,"%d/%m/%y %H:%M:%S"))&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;and then do the eval I get the results; however, it comes up in some number. I would like to get the difference in HH:MM.&lt;/P&gt;</description>
      <pubDate>Tue, 26 Jan 2021 21:05:58 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Time-Difference-and-Average/m-p/537304#M151878</guid>
      <dc:creator>geekf</dc:creator>
      <dc:date>2021-01-26T21:05:58Z</dc:date>
    </item>
    <item>
      <title>Re: Time Difference and Average</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Time-Difference-and-Average/m-p/537312#M151881</link>
      <description>&lt;P&gt;You are right, tostring converts this to string and then you couldn’t calculate average.&amp;nbsp;&lt;BR /&gt;you could first calculate (as your 2nd example shows) difference as seconds then after stats wit avg use eval to format it as you try to do in your 1st example.&amp;nbsp;&lt;BR /&gt;r. Ismo&lt;/P&gt;</description>
      <pubDate>Tue, 26 Jan 2021 21:20:30 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Time-Difference-and-Average/m-p/537312#M151881</guid>
      <dc:creator>isoutamo</dc:creator>
      <dc:date>2021-01-26T21:20:30Z</dc:date>
    </item>
    <item>
      <title>Re: Time Difference and Average</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Time-Difference-and-Average/m-p/537314#M151882</link>
      <description>&lt;P&gt;The number is in seconds, so you can use either of the last two statements from this example&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;| makeresults
| eval LastSeen="27/01/21 07:01:00", FirstSeen="27/01/21 05:11:22"
| eval time_dur=(strptime(LastSeen,"%d/%m/%y %H:%M:%S")-strptime(FirstSeen,"%d/%m/%y %H:%M:%S"))
| eval time_dur_str1=tostring(time_dur, "duration")
| eval time_dur_str2=printf("%02d:%02d", floor(time_dur / 3600), (time_dur % 3600) / 60)&lt;/LI-CODE&gt;&lt;P&gt;printf assumes that the hours is not more than 99 as it's only two places.&lt;/P&gt;&lt;P&gt;You could always use the tostring variant and trim the seconds off if you just want HH:MM&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 26 Jan 2021 21:27:19 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Time-Difference-and-Average/m-p/537314#M151882</guid>
      <dc:creator>bowesmana</dc:creator>
      <dc:date>2021-01-26T21:27:19Z</dc:date>
    </item>
    <item>
      <title>Re: Time Difference and Average</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Time-Difference-and-Average/m-p/537377#M151916</link>
      <description>&lt;P&gt;I am still unable to run avg&lt;/P&gt;&lt;P&gt;In your search, if I do&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;| stats avg(time_dur_str2)&lt;/LI-CODE&gt;&lt;P&gt;I don't see any results,&amp;nbsp; isn't it still a string?&lt;/P&gt;</description>
      <pubDate>Wed, 27 Jan 2021 09:04:59 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Time-Difference-and-Average/m-p/537377#M151916</guid>
      <dc:creator>geekf</dc:creator>
      <dc:date>2021-01-27T09:04:59Z</dc:date>
    </item>
    <item>
      <title>Re: Time Difference and Average</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Time-Difference-and-Average/m-p/537399#M151919</link>
      <description>&lt;P&gt;As i said earlier you must run stats avg(xxx) over numeric fields not a string.&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;....
| stats avg(time_dur) as tDur_as_seconds 
| eval time_duration_as_string = tostring(tDur_as_seconds, "duration")&lt;/LI-CODE&gt;</description>
      <pubDate>Wed, 27 Jan 2021 11:46:59 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Time-Difference-and-Average/m-p/537399#M151919</guid>
      <dc:creator>isoutamo</dc:creator>
      <dc:date>2021-01-27T11:46:59Z</dc:date>
    </item>
    <item>
      <title>Re: Time Difference and Average</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Time-Difference-and-Average/m-p/537422#M151928</link>
      <description>&lt;P&gt;If I use | table it works fine, if I use | stats avg(time_dur) it doesn't show the right values.&lt;/P&gt;</description>
      <pubDate>Wed, 27 Jan 2021 14:00:16 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Time-Difference-and-Average/m-p/537422#M151928</guid>
      <dc:creator>geekf</dc:creator>
      <dc:date>2021-01-27T14:00:16Z</dc:date>
    </item>
    <item>
      <title>Re: Time Difference and Average</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Time-Difference-and-Average/m-p/537423#M151929</link>
      <description>| stats avg(time_dur) AS time_dur_as_seconds&lt;BR /&gt;&lt;BR /&gt;shows average time duration in seconds. You must use&lt;BR /&gt;&lt;BR /&gt;| eval time_dur_as_string = tostring(time_dur_as_seconds, "duration")&lt;BR /&gt;&lt;BR /&gt;to get the real duration as human readable string (days+)hour:min:sec.&lt;BR /&gt;And if you want see it only as HH:MM then you could use:&lt;BR /&gt;&lt;BR /&gt;| eval time_dur_as_string =printf("%02d:%02d", floor(time_dur_as_seconds / 3600), (time_dur_as_seconds % 3600) / 60)&lt;BR /&gt;&lt;BR /&gt;as &lt;a href="https://community.splunk.com/t5/user/viewprofilepage/user-id/6367"&gt;@bowesmana&lt;/a&gt; propose.&lt;BR /&gt;And to present only it, please use&lt;BR /&gt;&lt;BR /&gt;| table time_dur_as_string&lt;BR /&gt;| rename time_dur_as_string AS "Time duration as (HH:SS)"</description>
      <pubDate>Wed, 27 Jan 2021 14:09:32 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Time-Difference-and-Average/m-p/537423#M151929</guid>
      <dc:creator>isoutamo</dc:creator>
      <dc:date>2021-01-27T14:09:32Z</dc:date>
    </item>
    <item>
      <title>Re: Time Difference and Average</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Time-Difference-and-Average/m-p/537425#M151930</link>
      <description>&lt;P&gt;Thank you for promptly responding to my question, I appreciate it.&lt;/P&gt;&lt;P&gt;I am attaching both the table and stats results, kindly let me know if there is something wrong here.&lt;/P&gt;</description>
      <pubDate>Wed, 27 Jan 2021 14:19:56 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Time-Difference-and-Average/m-p/537425#M151930</guid>
      <dc:creator>geekf</dc:creator>
      <dc:date>2021-01-27T14:19:56Z</dc:date>
    </item>
    <item>
      <title>Re: Time Difference and Average</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Time-Difference-and-Average/m-p/537429#M151931</link>
      <description>Can you add the whole SPL query here not only the two last line?</description>
      <pubDate>Wed, 27 Jan 2021 14:40:13 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Time-Difference-and-Average/m-p/537429#M151931</guid>
      <dc:creator>isoutamo</dc:creator>
      <dc:date>2021-01-27T14:40:13Z</dc:date>
    </item>
    <item>
      <title>Re: Time Difference and Average</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Time-Difference-and-Average/m-p/537433#M151933</link>
      <description>&lt;P&gt;Here are both of them:&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;index=main field=value1
| rex field=user "\w+\\\(?&amp;lt;user&amp;gt;.*)" 
| eval time=strftime(_time, "%d/%m/%y %H:%M:%S") 
| stats earliest(time) as FirstSeen, latest(time) as LastSeen by user 
| eval time_dur=(strptime(LastSeen,"%d/%m/%y %H:%M:%S")-strptime(FirstSeen,"%d/%m/%y %H:%M:%S")) 
| lookup some_user_lookup user AS user OUTPUT type 
| stats avg(time_dur) by type 
| eval time_dur=printf("%02d:%02d", floor(time_dur / 3600), (time_dur % 3600) / 60)&lt;/LI-CODE&gt;&lt;LI-CODE lang="markup"&gt;index=main field=value1
| rex field=user "\w+\\\(?&amp;lt;user&amp;gt;.*)" 
| eval time=strftime(_time, "%d/%m/%y %H:%M:%S") 
| stats earliest(time) as FirstSeen, latest(time) as LastSeen by user 
| eval time_dur=(strptime(LastSeen,"%d/%m/%y %H:%M:%S")-strptime(FirstSeen,"%d/%m/%y %H:%M:%S")) 
| lookup some_user_lookup user AS user OUTPUT type 
| eval time_dur=printf("%02d:%02d", floor(time_dur / 3600), (time_dur % 3600) / 60) 
| table type time_dur&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 27 Jan 2021 15:09:08 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Time-Difference-and-Average/m-p/537433#M151933</guid>
      <dc:creator>geekf</dc:creator>
      <dc:date>2021-01-27T15:09:08Z</dc:date>
    </item>
    <item>
      <title>Re: Time Difference and Average</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Time-Difference-and-Average/m-p/537499#M151960</link>
      <description>&lt;P&gt;Your problem is that when you do this&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;| stats avg(time_dur) by type &lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;the field name is no longer time_dur, it is avg(time_dur), so the next line will do nothing as there is no longer such a field.&amp;nbsp;&lt;/P&gt;&lt;P&gt;You need to do this&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;| stats avg(time_dur) as time_dur by type &lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;i.e. use the 'as' to rename the avg field name to the original.&lt;/P&gt;&lt;P&gt;then you can format it as needed.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 28 Jan 2021 06:49:54 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Time-Difference-and-Average/m-p/537499#M151960</guid>
      <dc:creator>bowesmana</dc:creator>
      <dc:date>2021-01-28T06:49:54Z</dc:date>
    </item>
    <item>
      <title>Re: Time Difference and Average</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Time-Difference-and-Average/m-p/537560#M151985</link>
      <description>&lt;P&gt;Thank you! This did the trick. If I may bother you again with one more question, how can I do the avg over a period of days? I tried this query but it didn't seem to work:&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;| bucket _time span=1d 
| stats count by _time &lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 28 Jan 2021 06:27:49 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Time-Difference-and-Average/m-p/537560#M151985</guid>
      <dc:creator>geekf</dc:creator>
      <dc:date>2021-01-28T06:27:49Z</dc:date>
    </item>
    <item>
      <title>Re: Time Difference and Average</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Time-Difference-and-Average/m-p/537573#M151991</link>
      <description>&lt;P&gt;It's probably worth posting another question on this, with some clearer details on what you want, for example, how do you want to handle FirstSeen as 2021-01-10 23:59 and LastSeen as 2021-01-11 00:01. If you bin by 1d, then there will be no LastSeen.&lt;/P&gt;&lt;P&gt;Also when you use 'count by', did you mean the average.&lt;/P&gt;&lt;P&gt;And your question of 'average' by day, is that also by type.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 28 Jan 2021 06:58:09 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Time-Difference-and-Average/m-p/537573#M151991</guid>
      <dc:creator>bowesmana</dc:creator>
      <dc:date>2021-01-28T06:58:09Z</dc:date>
    </item>
  </channel>
</rss>

