<?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 find second maximum value in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/How-to-find-second-maximum-value/m-p/644594#M223236</link>
    <description>&lt;P&gt;When I see a problem like this, my first instinct is to cheat. &amp;nbsp;This is simpler, and somewhat more semantic.&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;| makeresults | eval _raw="{
	\"Detail\": {
		\"Id\": 12345678,
		\"RequestCompleteTS\": \"2023-04-27T15:59:30.6960113-04:00\",
		\"Steps\": {
			\"0-step1\": 32,
			\"0-step2\": 15,
			\"3-step3\": 33,
			\"4-step4\": 49,
			\"5-step5\": 15,
			\"6-step6\": 9,
			\"7-step7\": 8
		},
		\"StepsCnt\": 18,
		\"TargetRegion\": \"BRD\"
	},
	\"LogType\": \"Info\",
	\"Message\": \"Success\",
	\"Time\": \"2023-04-27 15:59:30.696--04:00\"" 
  ``` ^^^ create dummy event ^^^ ```
| spath
| rename Detail.Id AS Id Detail.Steps.* AS *
| foreach *-step*
    [eval time_step = mvappend(time_step, printf("%04d", '&amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;') . ":&amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;")]
| eval time_step = mvsort(time_step)
| eval eval max = mvindex(time_step, mvcount(time_step) - 1), second_to_max = mvindex(time_step, mvcount(time_step) - 2)
| eval max = split(max, ":"), second_to_max = split(second_to_max, ":")
| eval "Step that is taking maximum time" = mvindex(second_to_max, 1), maxtime = tonumber(mvindex(second_to_max, 0))
| eval "Step that is taking next maximum time" = mvindex(second_to_max, 1), nextmaxtime = tonumber(mvindex(second_to_max, 0))
| table Id "Step that is taking maximum time" maxtime "Step that is taking next maximum time" nextmaxtime&lt;/LI-CODE&gt;&lt;P&gt;One key step is to pad 0 to the left of time with printf. &amp;nbsp;The above uses %04d. &amp;nbsp; You can add as many as your data requires.&lt;/P&gt;</description>
    <pubDate>Thu, 25 May 2023 07:40:10 GMT</pubDate>
    <dc:creator>yuanliu</dc:creator>
    <dc:date>2023-05-25T07:40:10Z</dc:date>
    <item>
      <title>How to find second maximum value</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-find-second-maximum-value/m-p/644576#M223231</link>
      <description>&lt;P&gt;I have a search like this to fetch the maximum value. Now the case i wanted to add is, if the maximum value field is 4-step4 then i want my table to display second maximum value as the max step.&lt;/P&gt;&lt;P&gt;Desired result&amp;nbsp;&lt;/P&gt;&lt;DIV&gt;&lt;TABLE border="1" cellspacing="0" cellpadding="0"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;&lt;P&gt;Id&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;Step that is taking maximum time&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;maxtime&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;P&gt;12345678&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;3-step3&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;33&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/DIV&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;| makeresults | eval _raw="{
	\"Detail\": {
		\"Id\": 12345678,
		\"RequestCompleteTS\": \"2023-04-27T15:59:30.6960113-04:00\",
		\"Steps\": {
			\"0-step1\": 32,
			\"0-step2\": 15,
			\"3-step3\": 33,
			\"4-step4\": 49,
			\"5-step5\": 15,
			\"6-step6\": 9,
			\"7-step7\": 8
		},
		\"StepsCnt\": 18,
		\"TargetRegion\": \"BRD\"
	},
	\"LogType\": \"Info\",
	\"Message\": \"Success\",
	\"Time\": \"2023-04-27 15:59:30.696--04:00\"" 
  ``` ^^^ create dummy event ^^^ ```
| spath
| rename Detail.Id AS Id Detail.Steps.* AS *
| foreach *step* [ eval Steps=if(isnull(Steps), '&amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;', mvappend(Steps, '&amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;')) ]
| eval steps_list_ordered=mvmap( mvsort(mvmap(mvdedup(Steps), len(Steps) . "-" . Steps)), substr(Steps, 3) )
      ,maxtime=mvindex(steps_list_ordered, mvcount(steps_list_ordered)-1)
| foreach *-step* [ eval "maxstep"=if('&amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;'=maxtime, "&amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;", if(isnotnull('maxstep'), 'maxstep', null())) ]
| rename maxstep AS "Step that is taking maximum time"
| table Id "Step that is taking maximum time" maxtime
​&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 25 May 2023 04:28:48 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-find-second-maximum-value/m-p/644576#M223231</guid>
      <dc:creator>sivaranjani</dc:creator>
      <dc:date>2023-05-25T04:28:48Z</dc:date>
    </item>
    <item>
      <title>Re: How to find second maximum value</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-find-second-maximum-value/m-p/644578#M223232</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://community.splunk.com/t5/user/viewprofilepage/user-id/256274"&gt;@sivaranjani&lt;/a&gt;&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;Just carry the existing logic through but for the next value.&lt;BR /&gt;&lt;BR /&gt;Like this...&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;| makeresults | eval _raw="{
	\"Detail\": {
		\"Id\": 12345678,
		\"RequestCompleteTS\": \"2023-04-27T15:59:30.6960113-04:00\",
		\"Steps\": {
			\"0-step1\": 32,
			\"0-step2\": 15,
			\"3-step3\": 33,
			\"4-step4\": 49,
			\"5-step5\": 15,
			\"6-step6\": 9,
			\"7-step7\": 8
		},
		\"StepsCnt\": 18,
		\"TargetRegion\": \"BRD\"
	},
	\"LogType\": \"Info\",
	\"Message\": \"Success\",
	\"Time\": \"2023-04-27 15:59:30.696--04:00\"" 
  ``` ^^^ create dummy event ^^^ ```
| spath
| rename Detail.Id AS Id Detail.Steps.* AS *
| foreach *step* [ eval Steps=if(isnull(Steps), '&amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;', mvappend(Steps, '&amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;')) ]
| eval steps_list_ordered=mvmap( mvsort(mvmap(mvdedup(Steps), len(Steps) . "-" . Steps)), substr(Steps, 3) )
      ,maxtime=mvindex(steps_list_ordered, mvcount(steps_list_ordered)-1)
      ,nextmaxtime=mvindex(steps_list_ordered, mvcount(steps_list_ordered)-2)
| foreach *-step* [ eval "maxstep"=if('&amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;'=maxtime, "&amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;", if(isnotnull('maxstep'), 'maxstep', null()))
                        ,"nextmaxstep"=if('&amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;'=nextmaxtime, "&amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;", if(isnotnull('nextmaxstep'), 'nextmaxstep', null()))
                  ]
| rename maxstep AS "Step that is taking maximum time" nextmaxstep AS "Step that is taking next maximum time"
| table Id "Step that is taking maximum time" maxtime "Step that is taking next maximum time" nextmaxtime&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;You may want to table or rename the output to match what you want exactly.&lt;BR /&gt;&lt;BR /&gt;Hope that helps&lt;/P&gt;</description>
      <pubDate>Thu, 25 May 2023 04:52:53 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-find-second-maximum-value/m-p/644578#M223232</guid>
      <dc:creator>yeahnah</dc:creator>
      <dc:date>2023-05-25T04:52:53Z</dc:date>
    </item>
    <item>
      <title>Re: How to find second maximum value</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-find-second-maximum-value/m-p/644594#M223236</link>
      <description>&lt;P&gt;When I see a problem like this, my first instinct is to cheat. &amp;nbsp;This is simpler, and somewhat more semantic.&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;| makeresults | eval _raw="{
	\"Detail\": {
		\"Id\": 12345678,
		\"RequestCompleteTS\": \"2023-04-27T15:59:30.6960113-04:00\",
		\"Steps\": {
			\"0-step1\": 32,
			\"0-step2\": 15,
			\"3-step3\": 33,
			\"4-step4\": 49,
			\"5-step5\": 15,
			\"6-step6\": 9,
			\"7-step7\": 8
		},
		\"StepsCnt\": 18,
		\"TargetRegion\": \"BRD\"
	},
	\"LogType\": \"Info\",
	\"Message\": \"Success\",
	\"Time\": \"2023-04-27 15:59:30.696--04:00\"" 
  ``` ^^^ create dummy event ^^^ ```
| spath
| rename Detail.Id AS Id Detail.Steps.* AS *
| foreach *-step*
    [eval time_step = mvappend(time_step, printf("%04d", '&amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;') . ":&amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;")]
| eval time_step = mvsort(time_step)
| eval eval max = mvindex(time_step, mvcount(time_step) - 1), second_to_max = mvindex(time_step, mvcount(time_step) - 2)
| eval max = split(max, ":"), second_to_max = split(second_to_max, ":")
| eval "Step that is taking maximum time" = mvindex(second_to_max, 1), maxtime = tonumber(mvindex(second_to_max, 0))
| eval "Step that is taking next maximum time" = mvindex(second_to_max, 1), nextmaxtime = tonumber(mvindex(second_to_max, 0))
| table Id "Step that is taking maximum time" maxtime "Step that is taking next maximum time" nextmaxtime&lt;/LI-CODE&gt;&lt;P&gt;One key step is to pad 0 to the left of time with printf. &amp;nbsp;The above uses %04d. &amp;nbsp; You can add as many as your data requires.&lt;/P&gt;</description>
      <pubDate>Thu, 25 May 2023 07:40:10 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-find-second-maximum-value/m-p/644594#M223236</guid>
      <dc:creator>yuanliu</dc:creator>
      <dc:date>2023-05-25T07:40:10Z</dc:date>
    </item>
    <item>
      <title>Re: How to find second maximum value</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-find-second-maximum-value/m-p/644680#M223250</link>
      <description>&lt;P&gt;Yeah, I like this,&amp;nbsp;&lt;a href="https://community.splunk.com/t5/user/viewprofilepage/user-id/33901"&gt;@yuanliu&lt;/a&gt;&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;I did pick up a few typos in your SPL that caused the max result to be the second max.&amp;nbsp; I've fixed it up and here it is for completeness...&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;...
| foreach *-step*
    [eval time_step = mvappend(time_step, printf("%04d", '&amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;') . ":&amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;")]
| eval time_step = mvsort(time_step)
      ,max = mvindex(time_step, mvcount(time_step) - 1)
      ,second_to_max = mvindex(time_step, mvcount(time_step) - 2)
      ,max = split(max, ":")
      ,second_to_max = split(second_to_max, ":")
      ,"Step that is taking maximum time" = mvindex(max, 1)
      ,maxtime = tonumber(mvindex(max, 0))
      ,"Step that is taking next maximum time" = mvindex(second_to_max, 1)
      ,nextmaxtime = tonumber(mvindex(second_to_max, 0))
| table Id "Step that is taking maximum time" maxtime "Step that is taking next maximum time" nextmaxtime&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 25 May 2023 21:04:16 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-find-second-maximum-value/m-p/644680#M223250</guid>
      <dc:creator>yeahnah</dc:creator>
      <dc:date>2023-05-25T21:04:16Z</dc:date>
    </item>
    <item>
      <title>Re: How to find second maximum value</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-find-second-maximum-value/m-p/644769#M223273</link>
      <description>&lt;P&gt;Thank you ! that helps.&lt;/P&gt;</description>
      <pubDate>Fri, 26 May 2023 16:53:56 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-find-second-maximum-value/m-p/644769#M223273</guid>
      <dc:creator>sivaranjani</dc:creator>
      <dc:date>2023-05-26T16:53:56Z</dc:date>
    </item>
  </channel>
</rss>

