<?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 add total median and min to median and min by month? in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/How-to-add-total-median-and-min-to-median-and-min-by-month/m-p/648038#M224243</link>
    <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://community.splunk.com/t5/user/viewprofilepage/user-id/257987"&gt;@qqzj&lt;/a&gt;,&lt;/P&gt;&lt;P&gt;Here's a way of getting two sets of different stats by using the &lt;A href="https://docs.splunk.com/Documentation/SplunkCloud/9.0.2303/SearchReference/Appendpipe" target="_self"&gt;appendpipe&lt;/A&gt; command:&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;| gentimes start=-217
| eval _time=starttime, mydata=-1*(random()%1000/100)
``` Create random test data ```

``` Calculate the stats for the whole data set ```
| appendpipe[|eval Month=strftime(_time,"%Y %m")  | stats count(mydata) AS nobs, mean(mydata) as mean, min(mydata) as min| eval Month="All"]

``` Calculate the stats for each month ```
| appendpipe [|eval Month=strftime(_time,"%Y %m") | stats count(mydata) AS nobs, mean(mydata) as mean, min(mydata) as min by Month]

``` Filter out the stats we just made, sorted reverse chronological order```
| search Month=*
| table Month, nobs, mean, min
| eval sort=if(Month="All", 0, Month)
| sort 0 - sort | fields - sort&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;It takes the output of the search so far, and runs the stats command. The resulting fields are added to the search result set.&lt;BR /&gt;&lt;BR /&gt;Next, we run stats on the output of the search again, but this time we calculate the monthly values.&lt;BR /&gt;&lt;BR /&gt;Finally, we throw away the source data and just select the stats we created.&lt;BR /&gt;&lt;BR /&gt;That gives you a table that looks like this:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="danspav_0-1687502334469.png" style="width: 999px;"&gt;&lt;img src="https://community.splunk.com/t5/image/serverpage/image-id/25991i962DB6AA0EBD7D5C/image-size/large?v=v2&amp;amp;px=999" role="button" title="danspav_0-1687502334469.png" alt="danspav_0-1687502334469.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;Cheers,&lt;BR /&gt;Daniel&lt;/P&gt;</description>
    <pubDate>Fri, 23 Jun 2023 06:40:35 GMT</pubDate>
    <dc:creator>danspav</dc:creator>
    <dc:date>2023-06-23T06:40:35Z</dc:date>
    <item>
      <title>How to add total median and min to median and min by month?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-add-total-median-and-min-to-median-and-min-by-month/m-p/647854#M224198</link>
      <description>&lt;P&gt;Hey guys!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I need the statistics of a bunch of data by month. And this is done already.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;search&lt;BR /&gt;|eval Month=strftime(_time,"%Y %m") | stats count(mydata) AS nobs, mean(mydata) as mean, min(mydata) as min by Month | reverse&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The output is what I want:&lt;/P&gt;&lt;TABLE border="0" width="348" cellspacing="0" cellpadding="0"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD width="87" height="21"&gt;Month&lt;/TD&gt;&lt;TD width="87"&gt;nobs&lt;/TD&gt;&lt;TD width="87"&gt;mean&lt;/TD&gt;&lt;TD width="87"&gt;min&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD height="21"&gt;2023 06&lt;/TD&gt;&lt;TD&gt;1900&lt;/TD&gt;&lt;TD&gt;-5.0239778&lt;/TD&gt;&lt;TD&gt;-68.73417&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD height="21"&gt;2023 05&lt;/TD&gt;&lt;TD&gt;3562&lt;/TD&gt;&lt;TD&gt;-4.2430259&lt;/TD&gt;&lt;TD&gt;-67.134697&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD height="21"&gt;2023 04&lt;/TD&gt;&lt;TD&gt;3181&lt;/TD&gt;&lt;TD&gt;-4.1811658&lt;/TD&gt;&lt;TD&gt;-64.995394&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD height="21"&gt;2023 03&lt;/TD&gt;&lt;TD&gt;4274&lt;/TD&gt;&lt;TD&gt;-4.3373071&lt;/TD&gt;&lt;TD&gt;-134.20177&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD height="21"&gt;2023 02&lt;/TD&gt;&lt;TD&gt;3939&lt;/TD&gt;&lt;TD&gt;-4.7725011&lt;/TD&gt;&lt;TD&gt;-73.538274&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD height="21"&gt;2023 01&lt;/TD&gt;&lt;TD&gt;2868&lt;/TD&gt;&lt;TD&gt;-5.5231115&lt;/TD&gt;&lt;TD&gt;-41.056093&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD height="21"&gt;2022 12&lt;/TD&gt;&lt;TD&gt;395&lt;/TD&gt;&lt;TD&gt;-4.617424&lt;/TD&gt;&lt;TD&gt;-35.51642&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Now I want to add another row at the bottom, called 'All', like this&lt;/P&gt;&lt;TABLE border="0" width="348" cellspacing="0" cellpadding="0"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD width="87" height="21"&gt;Month&lt;/TD&gt;&lt;TD width="87"&gt;nobs&lt;/TD&gt;&lt;TD width="87"&gt;mean&lt;/TD&gt;&lt;TD width="87"&gt;min&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD height="21"&gt;2023 06&lt;/TD&gt;&lt;TD&gt;1900&lt;/TD&gt;&lt;TD&gt;-5.0239778&lt;/TD&gt;&lt;TD&gt;-68.73417&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD height="21"&gt;2023 05&lt;/TD&gt;&lt;TD&gt;3562&lt;/TD&gt;&lt;TD&gt;-4.2430259&lt;/TD&gt;&lt;TD&gt;-67.134697&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD height="21"&gt;2023 04&lt;/TD&gt;&lt;TD&gt;3181&lt;/TD&gt;&lt;TD&gt;-4.1811658&lt;/TD&gt;&lt;TD&gt;-64.995394&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD height="21"&gt;2023 03&lt;/TD&gt;&lt;TD&gt;4274&lt;/TD&gt;&lt;TD&gt;-4.3373071&lt;/TD&gt;&lt;TD&gt;-134.20177&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD height="21"&gt;2023 02&lt;/TD&gt;&lt;TD&gt;3939&lt;/TD&gt;&lt;TD&gt;-4.7725011&lt;/TD&gt;&lt;TD&gt;-73.538274&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD height="21"&gt;2023 01&lt;/TD&gt;&lt;TD&gt;2868&lt;/TD&gt;&lt;TD&gt;-5.5231115&lt;/TD&gt;&lt;TD&gt;-41.056093&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD height="21"&gt;2022 12&lt;/TD&gt;&lt;TD&gt;395&lt;/TD&gt;&lt;TD&gt;-4.617424&lt;/TD&gt;&lt;TD&gt;-35.51642&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD height="21"&gt;All&lt;/TD&gt;&lt;TD&gt;z.zz&lt;/TD&gt;&lt;TD&gt;x.xx&lt;/TD&gt;&lt;TD&gt;y.yy&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here, the result for x.xx, y.yy and z.zz should be from the whole search result, instead of from the statistics shown in the rows. I am wondering how to do that? Ideally, I can use the search result without the need to search again and degrade performance. Thanks!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 21 Jun 2023 21:49:52 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-add-total-median-and-min-to-median-and-min-by-month/m-p/647854#M224198</guid>
      <dc:creator>qqzj</dc:creator>
      <dc:date>2023-06-21T21:49:52Z</dc:date>
    </item>
    <item>
      <title>Re: How to add total median and min to median and min by month?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-add-total-median-and-min-to-median-and-min-by-month/m-p/648038#M224243</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://community.splunk.com/t5/user/viewprofilepage/user-id/257987"&gt;@qqzj&lt;/a&gt;,&lt;/P&gt;&lt;P&gt;Here's a way of getting two sets of different stats by using the &lt;A href="https://docs.splunk.com/Documentation/SplunkCloud/9.0.2303/SearchReference/Appendpipe" target="_self"&gt;appendpipe&lt;/A&gt; command:&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;| gentimes start=-217
| eval _time=starttime, mydata=-1*(random()%1000/100)
``` Create random test data ```

``` Calculate the stats for the whole data set ```
| appendpipe[|eval Month=strftime(_time,"%Y %m")  | stats count(mydata) AS nobs, mean(mydata) as mean, min(mydata) as min| eval Month="All"]

``` Calculate the stats for each month ```
| appendpipe [|eval Month=strftime(_time,"%Y %m") | stats count(mydata) AS nobs, mean(mydata) as mean, min(mydata) as min by Month]

``` Filter out the stats we just made, sorted reverse chronological order```
| search Month=*
| table Month, nobs, mean, min
| eval sort=if(Month="All", 0, Month)
| sort 0 - sort | fields - sort&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;It takes the output of the search so far, and runs the stats command. The resulting fields are added to the search result set.&lt;BR /&gt;&lt;BR /&gt;Next, we run stats on the output of the search again, but this time we calculate the monthly values.&lt;BR /&gt;&lt;BR /&gt;Finally, we throw away the source data and just select the stats we created.&lt;BR /&gt;&lt;BR /&gt;That gives you a table that looks like this:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="danspav_0-1687502334469.png" style="width: 999px;"&gt;&lt;img src="https://community.splunk.com/t5/image/serverpage/image-id/25991i962DB6AA0EBD7D5C/image-size/large?v=v2&amp;amp;px=999" role="button" title="danspav_0-1687502334469.png" alt="danspav_0-1687502334469.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;Cheers,&lt;BR /&gt;Daniel&lt;/P&gt;</description>
      <pubDate>Fri, 23 Jun 2023 06:40:35 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-add-total-median-and-min-to-median-and-min-by-month/m-p/648038#M224243</guid>
      <dc:creator>danspav</dc:creator>
      <dc:date>2023-06-23T06:40:35Z</dc:date>
    </item>
    <item>
      <title>Re: How to add total median and min to median and min by month?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-add-total-median-and-min-to-median-and-min-by-month/m-p/648040#M224245</link>
      <description>&lt;P&gt;Actually, you could get simpler than that still.&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;| gentimes start=-217
| eval _time=starttime, mydata=-1*(random()%1000/100)
| eval Month=strftime(_time,"%Y %m") | stats count(mydata) AS nobs, mean(mydata) as mean, min(mydata) as min by Month | reverse
``` Create random test data ```

``` Work out Max, min, mean from the data ```
| eval means = nobs * mean
| appendpipe[|stats sum(nobs) as nobs, sum(means) as mean, min(min) as min| eval mean=mean/nobs, Month="All" ]
| fields - means&lt;/LI-CODE&gt;&lt;P&gt;Comparing the two methods, this one is accurate to twelve or so decimal places.&lt;/P&gt;</description>
      <pubDate>Fri, 23 Jun 2023 06:49:27 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-add-total-median-and-min-to-median-and-min-by-month/m-p/648040#M224245</guid>
      <dc:creator>danspav</dc:creator>
      <dc:date>2023-06-23T06:49:27Z</dc:date>
    </item>
    <item>
      <title>Re: How to add total median and min to median and min by month?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-add-total-median-and-min-to-median-and-min-by-month/m-p/648042#M224246</link>
      <description>&lt;P&gt;First look at the mathematics. &amp;nbsp;Total nobs is just a sum. &amp;nbsp;All time min is just minimum of all monthly minimums. &amp;nbsp;To calculate mean, you just sum up mean*nobs, then divide by total nobs. &amp;nbsp;In SPL, that is&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;search
| eval Month=strftime(_time,"%Y %m")
| stats count(mydata) AS nobs, mean(mydata) as mean, min(mydata) as min by Month
| reverse
| appendpipe
    [ stats sum(nobs) as nobs min(min) as min sum(eval(nobs * mean)) as mean
    | eval mean = mean / nobs ]&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 23 Jun 2023 06:56:01 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-add-total-median-and-min-to-median-and-min-by-month/m-p/648042#M224246</guid>
      <dc:creator>yuanliu</dc:creator>
      <dc:date>2023-06-23T06:56:01Z</dc:date>
    </item>
    <item>
      <title>Re: How to add total median and min to median and min by month?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-add-total-median-and-min-to-median-and-min-by-month/m-p/648236#M224312</link>
      <description>&lt;P&gt;Thanks for the help! I guess it is my fault that I did not include 'median' in the column in the example, even though I included median in the title of the question. Your method works for mean, min, and max. But not for median. I also need to deal with percentiles actually. So I need a way to direct use the result of the search, instead of use the results being processed once. Thanks!&lt;/P&gt;</description>
      <pubDate>Mon, 26 Jun 2023 17:18:36 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-add-total-median-and-min-to-median-and-min-by-month/m-p/648236#M224312</guid>
      <dc:creator>qqzj</dc:creator>
      <dc:date>2023-06-26T17:18:36Z</dc:date>
    </item>
    <item>
      <title>Re: How to add total median and min to median and min by month?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-add-total-median-and-min-to-median-and-min-by-month/m-p/648253#M224315</link>
      <description>&lt;P&gt;Hi danspav, THANK YOU!!!&lt;/P&gt;&lt;P&gt;I forgot to include 'median' as a column in the example, so your second, shorter, method does not work well for my purpose. But your first method works like magic!&lt;/P&gt;</description>
      <pubDate>Mon, 26 Jun 2023 19:19:43 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-add-total-median-and-min-to-median-and-min-by-month/m-p/648253#M224315</guid>
      <dc:creator>qqzj</dc:creator>
      <dc:date>2023-06-26T19:19:43Z</dc:date>
    </item>
    <item>
      <title>Re: How to add total median and min to median and min by month?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-add-total-median-and-min-to-median-and-min-by-month/m-p/648255#M224317</link>
      <description>&lt;P&gt;Hi danspav, I am very new to Splunk. So may I ask for a few extra helps?&lt;/P&gt;&lt;P&gt;1 What is the meaning of ' search Month=*'? I deleted it and the code still works. So I am not sure whether I should keep it or not.&lt;/P&gt;&lt;P&gt;2 What is the meaning of&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;sort 0 - sort&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;Thanks!&lt;/P&gt;</description>
      <pubDate>Mon, 26 Jun 2023 19:26:43 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-add-total-median-and-min-to-median-and-min-by-month/m-p/648255#M224317</guid>
      <dc:creator>qqzj</dc:creator>
      <dc:date>2023-06-26T19:26:43Z</dc:date>
    </item>
    <item>
      <title>Re: How to add total median and min to median and min by month?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-add-total-median-and-min-to-median-and-min-by-month/m-p/648262#M224322</link>
      <description>&lt;P&gt;sort 0 -sort. I think I figured this one out. sort 0 means including all records. -sort means using the descending order of the new variable created. Cool.&lt;/P&gt;</description>
      <pubDate>Mon, 26 Jun 2023 20:41:51 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-add-total-median-and-min-to-median-and-min-by-month/m-p/648262#M224322</guid>
      <dc:creator>qqzj</dc:creator>
      <dc:date>2023-06-26T20:41:51Z</dc:date>
    </item>
    <item>
      <title>Re: How to add total median and min to median and min by month?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-add-total-median-and-min-to-median-and-min-by-month/m-p/648284#M224328</link>
      <description>&lt;P&gt;Hi,&lt;BR /&gt;&lt;BR /&gt;Yes, sort 0 - sort will reverse sort by the "sort" field. The zero means sort all fields.&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;This bit:&lt;/P&gt;&lt;PRE&gt;| search Month=*&lt;/PRE&gt;&lt;P&gt;&lt;BR /&gt;Is used to filter out unneeded data. In our results set we have the raw data from the index and our calculated stats.&amp;nbsp; We only want the stats, so we search for all rows that have a field called Month that is not null.&lt;BR /&gt;If your search results look ok without it, then you can safely remove this bit.&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 27 Jun 2023 05:28:14 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-add-total-median-and-min-to-median-and-min-by-month/m-p/648284#M224328</guid>
      <dc:creator>danspav</dc:creator>
      <dc:date>2023-06-27T05:28:14Z</dc:date>
    </item>
  </channel>
</rss>

