<?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 get _time at median value? in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/How-to-get-time-at-median-value/m-p/328926#M166135</link>
    <description>&lt;P&gt;@cmerriman - Thanks!  I didn't expect to have to look up how splunk implemented a standard mathematical term.  Post has been updated and annotated, and I'm leaving my redundant brilliance in place for posterity, and as a warning to others. &lt;/P&gt;</description>
    <pubDate>Mon, 11 Sep 2017 19:36:04 GMT</pubDate>
    <dc:creator>DalJeanis</dc:creator>
    <dc:date>2017-09-11T19:36:04Z</dc:date>
    <item>
      <title>How to get _time at median value?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-get-time-at-median-value/m-p/328919#M166128</link>
      <description>&lt;P&gt;Hello everyone,&lt;/P&gt;

&lt;P&gt;Now, I encountered hard problem that I can't solve for long times. I was also google on many hours but not result. The problem has following:&lt;/P&gt;

&lt;P&gt;I wanna get median a value on search. I had that value by:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;source=check_request  app="test1"  | rename url as "URL"  | where URL="/ShippingOrder/Import" | stats median(el) as abc by URL
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;And I received a result following: &lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;URL                      abc
/ShippingOrder/Import   29250
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Yes, I got median value = &lt;CODE&gt;29250&lt;/CODE&gt;. But I wanna have add more a table that show &lt;CODE&gt;_time&lt;/CODE&gt; at event happen has &lt;CODE&gt;abc=29250&lt;/CODE&gt;. I searched following:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;source=check_request  app="test1"  | rename url as "URL"  | where URL="/ShippingOrder/Import" | stats median(el) as abc by URL | table URL abc _time
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;But table _time is blank.&lt;/P&gt;

&lt;P&gt;I found that time by:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;source=check_request  app="test1"  | rename url as "URL"  | where URL="/ShippingOrder/Import" | table URL _time el
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Result:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;URL                 el                 _time
/ShippingOrder/Import   29016   2017-09-10 18:08:58
/ShippingOrder/Import   6657    2017-09-10 16:47:58
/ShippingOrder/Import   11656   2017-09-10 16:11:35
/ShippingOrder/Import   23906   2017-09-10 14:46:58
/ShippingOrder/Import   46719   2017-09-10 11:03:56
/ShippingOrder/Import   15016   2017-09-10 16:54:22
/ShippingOrder/Import   29250   2017-09-10 16:46:22
/ShippingOrder/Import   51188   2017-09-10 14:58:22
/ShippingOrder/Import   44000   2017-09-10 14:51:22
/ShippingOrder/Import   12046   2017-09-10 14:42:22
/ShippingOrder/Import   50984   2017-09-10 14:41:22
/ShippingOrder/Import   39735   2017-09-10 14:25:22
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;And at the time has abc is median value &lt;CODE&gt;29250&lt;/CODE&gt; is &lt;CODE&gt;2017-09-10 16:46:22&lt;/CODE&gt;&lt;/P&gt;

&lt;P&gt;So, how to I get result following by some search?&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;URL                     abc               Time
/ShippingOrder/Import   29250   2017-09-10 16:46:22
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;I appreciate your reply&lt;/P&gt;</description>
      <pubDate>Mon, 11 Sep 2017 08:31:23 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-get-time-at-median-value/m-p/328919#M166128</guid>
      <dc:creator>luanvn</dc:creator>
      <dc:date>2017-09-11T08:31:23Z</dc:date>
    </item>
    <item>
      <title>Re: How to get _time at median value?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-get-time-at-median-value/m-p/328920#M166129</link>
      <description>&lt;P&gt;try this to see if it works for you: &lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt; source=check_request  app="test1"  | rename url as "URL"  | where URL="/ShippingOrder/Import"  | eventstats median(el) as abc by URL|eval medTime=if(abc=el,_time,null())|stats values(medTime) as _time values(abc) as abc by URL
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 11 Sep 2017 12:31:58 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-get-time-at-median-value/m-p/328920#M166129</guid>
      <dc:creator>cmerriman</dc:creator>
      <dc:date>2017-09-11T12:31:58Z</dc:date>
    </item>
    <item>
      <title>Re: How to get _time at median value?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-get-time-at-median-value/m-p/328921#M166130</link>
      <description>&lt;P&gt;That's great! It worked. Thanks so much cmerriman!&lt;/P&gt;</description>
      <pubDate>Mon, 11 Sep 2017 13:35:08 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-get-time-at-median-value/m-p/328921#M166130</guid>
      <dc:creator>luanvn</dc:creator>
      <dc:date>2017-09-11T13:35:08Z</dc:date>
    </item>
    <item>
      <title>Re: How to get _time at median value?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-get-time-at-median-value/m-p/328922#M166131</link>
      <description>&lt;P&gt;CORRECTION - In Splunk, the calculation of the aggregate function &lt;CODE&gt;median()&lt;/CODE&gt; does not match the mathematical definition, so the simpler code provided by @cmerriman is a complete solution.  Below is the code for how you would do the real median() if that were correctly calculated.&lt;/P&gt;

&lt;HR /&gt;

&lt;P&gt;Mathematically, median is not necessarily an actual number present in the dataset.  There could be only one, or there could be more than one, or there could be none.  Here's some examples to show why...&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt; 1 2 3 6 9        median is 3, which is present in the data
 1 2 3 3 3 6 9    median is 3, three present in the data
 1 2 2 4 6 9     mathematical median is 3, which is not present in the data.  However, splunk picks 4 instead.
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;HR /&gt;

&lt;P&gt;This following code is redundant under current implementation of &lt;CODE&gt;median()&lt;/CODE&gt; in splunk.  Just use @cmerriman's.&lt;/P&gt;

&lt;P&gt;That mathematical definition would make it a little funky to get the &lt;CODE&gt;_time&lt;/CODE&gt;, but it could be done.  What we would do, is calculate the DIFFERENCE from the median, and then pass any records that have the lowest difference.&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;source=check_request app="test1" url="/ShippingOrder/Import" 
| rename url as URL 
| eventstats median(el) as UrlMedianEl by URL
| eval DeltaToMedian= abs(UrlMedianEl - el)
| eventstats min(DeltatoMedian) as minDeltaToMedian by URL
| where DeltaToMedia = minDeltaToMedian 
| table _time URL el DeltaToMedian 
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;See, wasn't that tricky?  &lt;/P&gt;

&lt;HR /&gt;

&lt;P&gt;Examples altered - changed 4 and 5 to 6 and 9 respectively to help distinguish &lt;CODE&gt;median&lt;/CODE&gt; - the &lt;EM&gt;"middlest"&lt;/EM&gt; value - from &lt;CODE&gt;average&lt;/CODE&gt;.&lt;/P&gt;

&lt;P&gt;Also, the whole thing has become moot, because splunk's &lt;CODE&gt;median()&lt;/CODE&gt; just picks the higher of the two.&lt;/P&gt;</description>
      <pubDate>Mon, 11 Sep 2017 14:35:19 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-get-time-at-median-value/m-p/328922#M166131</guid>
      <dc:creator>DalJeanis</dc:creator>
      <dc:date>2017-09-11T14:35:19Z</dc:date>
    </item>
    <item>
      <title>Re: How to get _time at median value?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-get-time-at-median-value/m-p/328923#M166132</link>
      <description>&lt;P&gt;@luanvn, @cmerriman - you need a little more, because of the definition of median when there are even numbers of events.  You are only covering two of the three cases, and the third one should occur in roughly 50% of the searches.&lt;/P&gt;</description>
      <pubDate>Mon, 11 Sep 2017 14:37:48 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-get-time-at-median-value/m-p/328923#M166132</guid>
      <dc:creator>DalJeanis</dc:creator>
      <dc:date>2017-09-11T14:37:48Z</dc:date>
    </item>
    <item>
      <title>Re: How to get _time at median value?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-get-time-at-median-value/m-p/328924#M166133</link>
      <description>&lt;P&gt;@DalJeanis Thank for considering about that.&lt;/P&gt;</description>
      <pubDate>Mon, 11 Sep 2017 16:36:31 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-get-time-at-median-value/m-p/328924#M166133</guid>
      <dc:creator>luanvn</dc:creator>
      <dc:date>2017-09-11T16:36:31Z</dc:date>
    </item>
    <item>
      <title>Re: How to get _time at median value?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-get-time-at-median-value/m-p/328925#M166134</link>
      <description>&lt;P&gt;just for definition sake, median will grab the middle value of all present variables. not the middle value between min and max. &lt;BR /&gt;
&lt;A href="http://docs.splunk.com/Documentation/SplunkCloud/6.6.1/SearchReference/Aggregatefunctions#median.28X.29"&gt;http://docs.splunk.com/Documentation/SplunkCloud/6.6.1/SearchReference/Aggregatefunctions#median.28X.29&lt;/A&gt;&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;   |makeresults|eval data="1,2,3,5,6,7"| makemv data delim=","| mvexpand data | rename data as _raw | kv|stats median(_raw)
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;using this, you get a median of 5, not 4, because Splunk should pick the higher of the two middle values&lt;/P&gt;

&lt;P&gt;technically, if it is an even number, you're supposed to average the two middle numbers.&lt;BR /&gt;
&lt;A href="http://reference.wolfram.com/language/ref/Median.html"&gt;http://reference.wolfram.com/language/ref/Median.html&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 11 Sep 2017 17:39:41 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-get-time-at-median-value/m-p/328925#M166134</guid>
      <dc:creator>cmerriman</dc:creator>
      <dc:date>2017-09-11T17:39:41Z</dc:date>
    </item>
    <item>
      <title>Re: How to get _time at median value?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-get-time-at-median-value/m-p/328926#M166135</link>
      <description>&lt;P&gt;@cmerriman - Thanks!  I didn't expect to have to look up how splunk implemented a standard mathematical term.  Post has been updated and annotated, and I'm leaving my redundant brilliance in place for posterity, and as a warning to others. &lt;/P&gt;</description>
      <pubDate>Mon, 11 Sep 2017 19:36:04 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-get-time-at-median-value/m-p/328926#M166135</guid>
      <dc:creator>DalJeanis</dc:creator>
      <dc:date>2017-09-11T19:36:04Z</dc:date>
    </item>
  </channel>
</rss>

