{"id":23453,"date":"2020-10-13T17:47:38","date_gmt":"2020-10-13T17:47:38","guid":{"rendered":"https:\/\/merikebi.warrenmyers.com\/?p=23453"},"modified":"2020-10-13T17:47:38","modified_gmt":"2020-10-13T17:47:38","slug":"answer-by-warren-for-splunk-how-to-get-top-2-most-recent-values-for-each-group","status":"publish","type":"post","link":"https:\/\/merikebi.warrenmyers.com\/?p=23453","title":{"rendered":"Answer by warren for Splunk: How to get top 2 most recent values for each group"},"content":{"rendered":"<p>While @RichG&#8217;s <code>dedup<\/code> option may work, here&#8217;s one that uses <code>stats<\/code><\/p>\n<pre><code>index=ndx sourcetype=srctp clientType=* key=*\n| eval comb=_time+&quot; | &quot;+key\n| stats values(comb) as comb by clientType\n| eval mostrecent=mvindex(comb,-1), secondrecent=mvindex(comb,-2)\n| rex field=mostrecent &quot;(?&lt;timemost&gt;\\d+)\\s\\|\\s(?&lt;keymost&gt;.+)&quot;\n| rex field=secondrecent  &quot;(?&lt;timesecond&gt;\\d+)\\s\\|\\s(?&lt;keysecond&gt;.+)&quot;\n| table clientType timemost keymost timesecond keysecond\n| eval timemost=strftime(timemost,&quot;%c&quot;), timesecond=strftime(timesecond,&quot;%c&quot;)\n<\/code><\/pre>\n<p>from User warren &#8211; Stack Overflow https:\/\/stackoverflow.com\/questions\/64336226\/splunk-how-to-get-top-2-most-recent-values-for-each-group\/64340338#64340338<br \/>\nvia <a href=\"https:\/\/ifttt.com\/?ref=da&#038;site=wordpress\">IFTTT<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>While @RichG&#8217;s dedup option may work, here&#8217;s one that uses stats index=ndx sourcetype=srctp clientType=* key=* | eval comb=_time+&quot; | &quot;+key | stats values(comb) as comb by clientType | eval mostrecent=mvindex(comb,-1), secondrecent=mvindex(comb,-2) | rex field=mostrecent &quot;(?&lt;timemost&gt;\\d+)\\s\\|\\s(?&lt;keymost&gt;.+)&quot; | rex field=secondrecent &quot;(?&lt;timesecond&gt;\\d+)\\s\\|\\s(?&lt;keysecond&gt;.+)&quot; | table clientType timemost keymost timesecond keysecond | eval timemost=strftime(timemost,&quot;%c&quot;), timesecond=strftime(timesecond,&quot;%c&quot;) from User warren &#8211; Stack &hellip;<br \/><a href=\"https:\/\/merikebi.warrenmyers.com\/?p=23453\" class=\"more-link pen_button pen_element_default pen_icon_arrow_double\">Continue reading <span class=\"screen-reader-text\">Answer by warren for Splunk: How to get top 2 most recent values for each group<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[4],"tags":[991],"keyring_services":[],"class_list":["post-23453","post","type-post","status-publish","format-standard","hentry","category-blih","tag-stackexchange"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/merikebi.warrenmyers.com\/index.php?rest_route=\/wp\/v2\/posts\/23453","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/merikebi.warrenmyers.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/merikebi.warrenmyers.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/merikebi.warrenmyers.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/merikebi.warrenmyers.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=23453"}],"version-history":[{"count":1,"href":"https:\/\/merikebi.warrenmyers.com\/index.php?rest_route=\/wp\/v2\/posts\/23453\/revisions"}],"predecessor-version":[{"id":23454,"href":"https:\/\/merikebi.warrenmyers.com\/index.php?rest_route=\/wp\/v2\/posts\/23453\/revisions\/23454"}],"wp:attachment":[{"href":"https:\/\/merikebi.warrenmyers.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=23453"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/merikebi.warrenmyers.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=23453"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/merikebi.warrenmyers.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=23453"},{"taxonomy":"keyring_services","embeddable":true,"href":"https:\/\/merikebi.warrenmyers.com\/index.php?rest_route=%2Fwp%2Fv2%2Fkeyring_services&post=23453"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}