<?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 How to sort data in stats command output in Splunk Enterprise</title>
    <link>https://community.splunk.com/t5/Splunk-Enterprise/How-to-sort-data-in-stats-command-output/m-p/708116#M21260</link>
    <description>&lt;P&gt;Dear All,&lt;/P&gt;&lt;P&gt;Kindly suggest , How to sort data in stats command output as per event time .&lt;/P&gt;&lt;P&gt;Example:&amp;nbsp;&lt;BR /&gt;Requirement : VPN login details as per source user&amp;nbsp;In last one hour.&lt;/P&gt;&lt;P&gt;SPL Query :&lt;BR /&gt;index="network" sourcetype=vpn&lt;BR /&gt;| eval "Login_Time" = strftime(_time, "%m/%d/%Y %I:%M:%S %p")&lt;BR /&gt;| stats values(SourceUser)as User&amp;nbsp; values(Login_Time) as&amp;nbsp;VPN_Login_Time count by _time host&lt;/P&gt;&lt;TABLE width="648"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD width="89"&gt;Date&lt;/TD&gt;&lt;TD width="89"&gt;Host&lt;/TD&gt;&lt;TD width="217"&gt;User&lt;/TD&gt;&lt;TD width="189"&gt;VPN Login Time&lt;/TD&gt;&lt;TD width="64"&gt;Count&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1/7/2025 0:00&lt;/TD&gt;&lt;TD&gt;10.10.8.45&lt;/TD&gt;&lt;TD width="217"&gt;Amar&lt;BR /&gt;Rajesh&lt;BR /&gt;Zainab&lt;/TD&gt;&lt;TD width="189"&gt;01/07/2025 06:01:25 AM&lt;BR /&gt;01/7/2025&amp;nbsp; 06:30:21 AM&lt;BR /&gt;01/7/2025&amp;nbsp; 06:50:49 AM&lt;/TD&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&lt;BR /&gt;challenge in above example output,&amp;nbsp; Amar was logged in at&amp;nbsp;01/7/2025&amp;nbsp; 06:30:21AM and&amp;nbsp; Zainab&amp;nbsp;was logged in at&amp;nbsp;01/07/2025 06:01:25 AM but in output result user were sorted with alphabetical order and Login time were&amp;nbsp; sorted in descending order.&amp;nbsp; And User field can not be added as third field in by expression .&lt;/P&gt;</description>
    <pubDate>Tue, 07 Jan 2025 07:27:36 GMT</pubDate>
    <dc:creator>Nraj87</dc:creator>
    <dc:date>2025-01-07T07:27:36Z</dc:date>
    <item>
      <title>How to sort data in stats command output</title>
      <link>https://community.splunk.com/t5/Splunk-Enterprise/How-to-sort-data-in-stats-command-output/m-p/708116#M21260</link>
      <description>&lt;P&gt;Dear All,&lt;/P&gt;&lt;P&gt;Kindly suggest , How to sort data in stats command output as per event time .&lt;/P&gt;&lt;P&gt;Example:&amp;nbsp;&lt;BR /&gt;Requirement : VPN login details as per source user&amp;nbsp;In last one hour.&lt;/P&gt;&lt;P&gt;SPL Query :&lt;BR /&gt;index="network" sourcetype=vpn&lt;BR /&gt;| eval "Login_Time" = strftime(_time, "%m/%d/%Y %I:%M:%S %p")&lt;BR /&gt;| stats values(SourceUser)as User&amp;nbsp; values(Login_Time) as&amp;nbsp;VPN_Login_Time count by _time host&lt;/P&gt;&lt;TABLE width="648"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD width="89"&gt;Date&lt;/TD&gt;&lt;TD width="89"&gt;Host&lt;/TD&gt;&lt;TD width="217"&gt;User&lt;/TD&gt;&lt;TD width="189"&gt;VPN Login Time&lt;/TD&gt;&lt;TD width="64"&gt;Count&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1/7/2025 0:00&lt;/TD&gt;&lt;TD&gt;10.10.8.45&lt;/TD&gt;&lt;TD width="217"&gt;Amar&lt;BR /&gt;Rajesh&lt;BR /&gt;Zainab&lt;/TD&gt;&lt;TD width="189"&gt;01/07/2025 06:01:25 AM&lt;BR /&gt;01/7/2025&amp;nbsp; 06:30:21 AM&lt;BR /&gt;01/7/2025&amp;nbsp; 06:50:49 AM&lt;/TD&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&lt;BR /&gt;challenge in above example output,&amp;nbsp; Amar was logged in at&amp;nbsp;01/7/2025&amp;nbsp; 06:30:21AM and&amp;nbsp; Zainab&amp;nbsp;was logged in at&amp;nbsp;01/07/2025 06:01:25 AM but in output result user were sorted with alphabetical order and Login time were&amp;nbsp; sorted in descending order.&amp;nbsp; And User field can not be added as third field in by expression .&lt;/P&gt;</description>
      <pubDate>Tue, 07 Jan 2025 07:27:36 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Enterprise/How-to-sort-data-in-stats-command-output/m-p/708116#M21260</guid>
      <dc:creator>Nraj87</dc:creator>
      <dc:date>2025-01-07T07:27:36Z</dc:date>
    </item>
    <item>
      <title>Re: How to sort data in stats command output</title>
      <link>https://community.splunk.com/t5/Splunk-Enterprise/How-to-sort-data-in-stats-command-output/m-p/708119#M21261</link>
      <description>&lt;P class="lia-align-left"&gt;If you want to end up with several multivalue fields that are correlated with each other, you can't use stats &lt;STRONG&gt;values()&amp;nbsp;&lt;/STRONG&gt;as the output from a values() aggregation is always in sorted order.&lt;/P&gt;&lt;P class="lia-align-left"&gt;There are a number of options&lt;/P&gt;&lt;P class="lia-align-left"&gt;1. Use stats list() which will record the item for EVERY event but the order is preserved, but of course if you have duplicates for the same user on the same _time, you will have multiple entries. Note that &lt;STRONG&gt;list&lt;/STRONG&gt;() has a maximum list length of 100 items&lt;/P&gt;&lt;P class="lia-align-left"&gt;2. Make a combination field of the items you want to end up with and use stats values(new_field) and then split them out again, e.g. like this&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;...
| eval _tmp=SourceUser."###".Login_Time
| stats values(_tmp) as _tmp count by _time host
| rex field=_tmp max_match=0 "(?&amp;lt;User&amp;gt;.*)###(?&amp;lt;VPN_Login_Time&amp;gt;.*)"
| fields - _tmp&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P class="lia-align-left"&gt;3. Do this to handle the potential duplicate logins on the same _time for the same user&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;...
| stats values(Login_Time) as VPN_Login_Time count by _time host SourceUser
| stats list(*) as * sum(count) as count by _time host&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;so include the SourceUser initially then use stats list finally&lt;/P&gt;&lt;P&gt;Hope this helps&lt;/P&gt;&lt;P class="lia-align-left"&gt;&amp;nbsp;&lt;/P&gt;&lt;P class="lia-align-left"&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 07 Jan 2025 09:00:39 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Enterprise/How-to-sort-data-in-stats-command-output/m-p/708119#M21261</guid>
      <dc:creator>bowesmana</dc:creator>
      <dc:date>2025-01-07T09:00:39Z</dc:date>
    </item>
  </channel>
</rss>

