<?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: Sorting Question in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/Sorting-Question/m-p/378214#M110895</link>
    <description>&lt;P&gt;When you do a &lt;CODE&gt;| stats values(field) AS fieldnew&lt;/CODE&gt; you can also simply do a &lt;CODE&gt;| sort 0 -fieldnew&lt;/CODE&gt; at the end which should be the easiest solution.&lt;BR /&gt;
Edit: Oh, and &lt;CODE&gt;stats()&lt;/CODE&gt; also has two functions called &lt;CODE&gt;first()&lt;/CODE&gt; and &lt;CODE&gt;last()&lt;/CODE&gt;.&lt;/P&gt;

&lt;P&gt;Skalli&lt;/P&gt;</description>
    <pubDate>Sat, 18 May 2019 13:16:52 GMT</pubDate>
    <dc:creator>skalliger</dc:creator>
    <dc:date>2019-05-18T13:16:52Z</dc:date>
    <item>
      <title>Sorting Question</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Sorting-Question/m-p/378213#M110894</link>
      <description>&lt;P&gt;Hello, hoping someone can give me a hand or point me in the right direction.&lt;BR /&gt;
I have a report that is based off of a DB query,  I'm trying to sort a date field descending. Due to the nature of the data and the number of records I need to display, I end up with duplicate date records. This comes into play later on.........&lt;/P&gt;

&lt;P&gt;In order to correctly sort the date field, I am converting the field to epoch time sorting it desc and then converting back to human readable ( "y/m/d")&lt;BR /&gt;
When I run the query everything works as expected, see below for search details to this point:&lt;/P&gt;

&lt;P&gt;|"Base SQL query"&lt;BR /&gt;
| eval TRIGGER_DATE = strptime(TRIGGER_DATE, "%Y-%m-%d %H:%M:%S.%3N")&lt;BR /&gt;
| sort 0 -num(TRIGGER_DATE)&lt;BR /&gt;
| eval TRIGGER_DATE =strftime(TRIGGER_DATE,"%Y/%m/%d")&lt;/P&gt;

&lt;P&gt;Here's where things go wrong. &lt;BR /&gt;
Next I am piping the results to a stats command. If I use list(Date_field) everything is fine, dates are sorted correctly but I have multiple identical date fields. &lt;BR /&gt;
If I use values(date_field), the duplicates are handled but the dates are now sorted ascending.&lt;/P&gt;

&lt;P&gt;|"Base SQL query"&lt;BR /&gt;
| eval TRIGGER_DATE = strptime(TRIGGER_DATE, "%Y-%m-%d %H:%M:%S.%3N")&lt;BR /&gt;
| sort 0 -num(TRIGGER_DATE)&lt;BR /&gt;
| eval TRIGGER_DATE =strftime(TRIGGER_DATE,"%Y/%m/%d")&lt;BR /&gt;
| stats values(EXT_ACCT), values(date_field)......etc&lt;/P&gt;

&lt;P&gt;I have tried everything I can think of but cannot get the date fields sorted descending after the stats command when using values.&lt;/P&gt;

&lt;P&gt;Any help would be greatly appreciated.&lt;/P&gt;</description>
      <pubDate>Wed, 30 Sep 2020 00:35:54 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Sorting-Question/m-p/378213#M110894</guid>
      <dc:creator>g038123</dc:creator>
      <dc:date>2020-09-30T00:35:54Z</dc:date>
    </item>
    <item>
      <title>Re: Sorting Question</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Sorting-Question/m-p/378214#M110895</link>
      <description>&lt;P&gt;When you do a &lt;CODE&gt;| stats values(field) AS fieldnew&lt;/CODE&gt; you can also simply do a &lt;CODE&gt;| sort 0 -fieldnew&lt;/CODE&gt; at the end which should be the easiest solution.&lt;BR /&gt;
Edit: Oh, and &lt;CODE&gt;stats()&lt;/CODE&gt; also has two functions called &lt;CODE&gt;first()&lt;/CODE&gt; and &lt;CODE&gt;last()&lt;/CODE&gt;.&lt;/P&gt;

&lt;P&gt;Skalli&lt;/P&gt;</description>
      <pubDate>Sat, 18 May 2019 13:16:52 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Sorting-Question/m-p/378214#M110895</guid>
      <dc:creator>skalliger</dc:creator>
      <dc:date>2019-05-18T13:16:52Z</dc:date>
    </item>
    <item>
      <title>Re: Sorting Question</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Sorting-Question/m-p/378215#M110896</link>
      <description>&lt;P&gt;&lt;a href="https://community.splunk.com/t5/user/viewprofilepage/user-id/78251"&gt;@g038123&lt;/a&gt; what is that you want to output as result? Also do you have three date fields TRIGGER_DATE, EXT_ACCT and something else? Are you performing stats by some field?&lt;/P&gt;</description>
      <pubDate>Wed, 30 Sep 2020 00:37:39 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Sorting-Question/m-p/378215#M110896</guid>
      <dc:creator>niketn</dc:creator>
      <dc:date>2020-09-30T00:37:39Z</dc:date>
    </item>
    <item>
      <title>Re: Sorting Question</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Sorting-Question/m-p/378216#M110897</link>
      <description>&lt;P&gt;So you have the results sorted in the order you want them, but you want to eliminate duplicates after you pull them together into a multi-valued field using stats? So for a run-anywhere example, we have a bunch of results with dates:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| makeresults count=20 
| eval random=random()%3-1,adj=case(random=0,"@d",random=1,"+d@d",random=-1,"-d@d"),date=strftime(relative_time(_time,adj),"%Y-%m-%d") 
| table date 
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;As you found, if you sort the dates in the order that you want them going into the stats command, you can use list to then collect the list of dates. Then after the stats command you can use &lt;A href="https://docs.splunk.com/Documentation/Splunk/7.2.6/SearchReference/MultivalueEvalFunctions#Description_4"&gt;the mvdedup function&lt;/A&gt; to eliminate the duplicate entries.&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;...
| sort 0 - date | stats list(date) as list
| eval list=mvdedup(list)
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;I would take the opportunity to log an enhancement request with Splunk... it would be nice if there was an option to just reverse the output of values, (saving you from the pre-sort) but I'm not seeing an option that would do such easily.&lt;/P&gt;</description>
      <pubDate>Sun, 19 May 2019 00:02:47 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Sorting-Question/m-p/378216#M110897</guid>
      <dc:creator>acharlieh</dc:creator>
      <dc:date>2019-05-19T00:02:47Z</dc:date>
    </item>
    <item>
      <title>Re: Sorting Question</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Sorting-Question/m-p/378217#M110898</link>
      <description>&lt;P&gt;Thank you acharlieh!&lt;BR /&gt;
The mvdedup works perfectly! Just what I needed.&lt;/P&gt;</description>
      <pubDate>Mon, 20 May 2019 15:54:15 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Sorting-Question/m-p/378217#M110898</guid>
      <dc:creator>g038123</dc:creator>
      <dc:date>2019-05-20T15:54:15Z</dc:date>
    </item>
  </channel>
</rss>

