<?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: stats or tstats latest not working on array fields in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/Why-are-stats-or-tstats-latest-not-working-on-array-fields/m-p/592441#M206199</link>
    <description>&lt;P&gt;In my case, the fields are coming from different kinds of events and I am compiling them together. So can’t do head 1 etc as the latest of one field maybe in an older event, while latest of &amp;nbsp;another may be in the latest event. Etc.&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;Thanks for the suggestion though. Definitely useful to do when all the fields are in all events that i am looking at.&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 05 Apr 2022 13:57:54 GMT</pubDate>
    <dc:creator>shikhanshua</dc:creator>
    <dc:date>2022-04-05T13:57:54Z</dc:date>
    <item>
      <title>Why are stats or tstats latest not working on array fields?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Why-are-stats-or-tstats-latest-not-working-on-array-fields/m-p/592024#M206085</link>
      <description>&lt;P&gt;I have events like these (just some made-up data), that are pushed in JSON format to Splunk:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="javascript"&gt;{"name":"abc", "grade":"third", "result": "PASS", "courses":["math","science","literature"], "interests":["this","that"]}&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Events are being generated all the time, and I need to get the latest values of "result",&amp;nbsp;"courses" and "interests" for a given "name" and "grade". Note that "courses" and "interests" are lists/arrays, while other fields are strings.&lt;/P&gt;
&lt;P&gt;So I am doing somethings like:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="markup"&gt;index=whatever name=abc grade=third | stats latest(courses) as courses, latest(interests) as interests, latest(result) as result

index=whatever name=abc grade=third | stats latest(courses{}) as courses, latest(interests{}) as interests, latest(result) as result

index=whatever name=abc grade=third | eval courses=json_array_to_mv(courses), interests=json_array_to_mv(interests) | stats latest(courses) as courses, latest(interests) as interests, latest(result) as result&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Also tried with "tstats" approach.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;None of those work. I get the courses and interests as empty values. result comes in fine, because its a string.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;How can I get the "latest" lists of courses and interests given other values?&lt;/P&gt;</description>
      <pubDate>Mon, 04 Apr 2022 14:38:58 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Why-are-stats-or-tstats-latest-not-working-on-array-fields/m-p/592024#M206085</guid>
      <dc:creator>shikhanshua</dc:creator>
      <dc:date>2022-04-04T14:38:58Z</dc:date>
    </item>
    <item>
      <title>Re: stats or tstats latest not working on array fields</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Why-are-stats-or-tstats-latest-not-working-on-array-fields/m-p/592033#M206089</link>
      <description>&lt;P&gt;This works for me:&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;| makeresults | eval _raw="{\"name\":\"abc\", \"grade\":\"third\", \"result\": \"PASS\", \"courses\":[\"math\",\"science\",\"literature\"], \"interests\":[\"this\",\"that\"]}" | kv

| eval courses=mvjoin('courses{}', ", "), interests=mvjoin('interests{}', ", ")
| stats latest(courses) as courses, latest(interests) as interests, latest(result) as result&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;| eval courses=mvjoin('courses{}', ", "), interests=mvjoin('interests{}', ", ")&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="VatsalJagani_0-1648878536737.png" style="width: 581px;"&gt;&lt;img src="https://community.splunk.com/t5/image/serverpage/image-id/18895i853F23C42755F965/image-dimensions/581x122?v=v2" width="581" height="122" role="button" title="VatsalJagani_0-1648878536737.png" alt="VatsalJagani_0-1648878536737.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;Kindly accept the answer if it resolves your problem!!&lt;/P&gt;</description>
      <pubDate>Sat, 02 Apr 2022 05:50:23 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Why-are-stats-or-tstats-latest-not-working-on-array-fields/m-p/592033#M206089</guid>
      <dc:creator>VatsalJagani</dc:creator>
      <dc:date>2022-04-02T05:50:23Z</dc:date>
    </item>
    <item>
      <title>Re: stats or tstats latest not working on array fields</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Why-are-stats-or-tstats-latest-not-working-on-array-fields/m-p/592038#M206091</link>
      <description>&lt;P&gt;As&amp;nbsp;&lt;a href="https://community.splunk.com/t5/user/viewprofilepage/user-id/93915"&gt;@VatsalJagani&lt;/a&gt;&amp;nbsp;demonstrated, you can bind array into a single string for use with stats. &amp;nbsp;But you don't need stats to get "latest" of every field if the sole goal is to get the latest. &amp;nbsp;Here are some thoughts.&lt;/P&gt;&lt;OL&gt;&lt;LI&gt;In most indexes, data are stored in reverse time order already. &amp;nbsp;All you need to do is to retrieve the last row, i.e., "&lt;FONT face="courier new,courier"&gt;| head 1&lt;/FONT&gt;"&lt;/LI&gt;&lt;LI&gt;If time in index may be scrambled, sort first, i.e., "&lt;FONT face="courier new,courier"&gt;| sort _time | tail 1&lt;/FONT&gt;"&lt;/LI&gt;&lt;LI&gt;If you want to get the latest by some key, you can use dedup, e.g., "&lt;FONT face="courier new,courier"&gt;| dedup name&lt;/FONT&gt;" (again, if index may contain out of order events, sort first, e.g., "&lt;FONT face="courier new,courier"&gt;| sort - _time | dedup name&lt;/FONT&gt;".)&lt;/LI&gt;&lt;/OL&gt;&lt;P&gt;Here are some simulated data&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;_time&lt;/TD&gt;&lt;TD&gt;&lt;DIV class=""&gt;courses{}&lt;/DIV&gt;&lt;/TD&gt;&lt;TD&gt;grade&lt;/TD&gt;&lt;TD&gt;&lt;DIV class=""&gt;interests{}&lt;/DIV&gt;&lt;/TD&gt;&lt;TD&gt;name&lt;/TD&gt;&lt;TD&gt;result&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;2022-04-02 06:07:01&lt;/TD&gt;&lt;TD&gt;&lt;DIV class=""&gt;math&lt;/DIV&gt;&lt;DIV class=""&gt;science&lt;/DIV&gt;&lt;DIV class=""&gt;literature&lt;/DIV&gt;&lt;/TD&gt;&lt;TD&gt;fourth&lt;/TD&gt;&lt;TD&gt;&lt;DIV class=""&gt;this&lt;/DIV&gt;&lt;DIV class=""&gt;that&lt;/DIV&gt;&lt;/TD&gt;&lt;TD&gt;abc&lt;/TD&gt;&lt;TD&gt;PASS&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;2022-04-02 06:04:01&lt;/TD&gt;&lt;TD&gt;&lt;DIV class=""&gt;arts&lt;/DIV&gt;&lt;DIV class=""&gt;science&lt;/DIV&gt;&lt;DIV class=""&gt;literature&lt;/DIV&gt;&lt;/TD&gt;&lt;TD&gt;second&lt;/TD&gt;&lt;TD&gt;&lt;DIV class=""&gt;thing&lt;/DIV&gt;&lt;DIV class=""&gt;matter&lt;/DIV&gt;&lt;/TD&gt;&lt;TD&gt;def&lt;/TD&gt;&lt;TD&gt;PASS&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;2022-04-02 06:01:01&lt;/TD&gt;&lt;TD&gt;&lt;DIV class=""&gt;math&lt;/DIV&gt;&lt;DIV class=""&gt;science&lt;/DIV&gt;&lt;DIV class=""&gt;PE&lt;/DIV&gt;&lt;/TD&gt;&lt;TD&gt;third&lt;/TD&gt;&lt;TD&gt;&lt;DIV class=""&gt;here&lt;/DIV&gt;&lt;DIV class=""&gt;there&lt;/DIV&gt;&lt;/TD&gt;&lt;TD&gt;abc&lt;/TD&gt;&lt;TD&gt;FAIL&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;Using simple head, you get the first row&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;_time&lt;/TD&gt;&lt;TD&gt;&lt;DIV class=""&gt;courses{}&lt;/DIV&gt;&lt;/TD&gt;&lt;TD&gt;grade&lt;/TD&gt;&lt;TD&gt;&lt;DIV class=""&gt;interests{}&lt;/DIV&gt;&lt;/TD&gt;&lt;TD&gt;name&lt;/TD&gt;&lt;TD&gt;result&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;2022-04-02 06:12:09&lt;/TD&gt;&lt;TD&gt;&lt;DIV class=""&gt;math&lt;/DIV&gt;&lt;DIV class=""&gt;science&lt;/DIV&gt;&lt;DIV class=""&gt;literature&lt;/DIV&gt;&lt;/TD&gt;&lt;TD&gt;fourth&lt;/TD&gt;&lt;TD&gt;&lt;DIV class=""&gt;this&lt;/DIV&gt;&lt;DIV class=""&gt;that&lt;/DIV&gt;&lt;/TD&gt;&lt;TD&gt;abc&lt;/TD&gt;&lt;TD&gt;PASS&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;Using dedup name, the output is&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;_time&lt;/TD&gt;&lt;TD&gt;&lt;DIV class=""&gt;courses{}&lt;/DIV&gt;&lt;/TD&gt;&lt;TD&gt;grade&lt;/TD&gt;&lt;TD&gt;&lt;DIV class=""&gt;interests{}&lt;/DIV&gt;&lt;/TD&gt;&lt;TD&gt;name&lt;/TD&gt;&lt;TD&gt;result&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;2022-04-02 06:13:30&lt;/TD&gt;&lt;TD&gt;&lt;DIV class=""&gt;math&lt;/DIV&gt;&lt;DIV class=""&gt;science&lt;/DIV&gt;&lt;DIV class=""&gt;literature&lt;/DIV&gt;&lt;/TD&gt;&lt;TD&gt;fourth&lt;/TD&gt;&lt;TD&gt;&lt;DIV class=""&gt;this&lt;/DIV&gt;&lt;DIV class=""&gt;that&lt;/DIV&gt;&lt;/TD&gt;&lt;TD&gt;abc&lt;/TD&gt;&lt;TD&gt;PASS&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;2022-04-02 06:10:30&lt;/TD&gt;&lt;TD&gt;&lt;DIV class=""&gt;arts&lt;/DIV&gt;&lt;DIV class=""&gt;science&lt;/DIV&gt;&lt;DIV class=""&gt;literature&lt;/DIV&gt;&lt;/TD&gt;&lt;TD&gt;second&lt;/TD&gt;&lt;TD&gt;&lt;DIV class=""&gt;thing&lt;/DIV&gt;&lt;DIV class=""&gt;matter&lt;/DIV&gt;&lt;/TD&gt;&lt;TD&gt;def&lt;/TD&gt;&lt;TD&gt;PASS&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;</description>
      <pubDate>Sat, 02 Apr 2022 06:19:20 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Why-are-stats-or-tstats-latest-not-working-on-array-fields/m-p/592038#M206091</guid>
      <dc:creator>yuanliu</dc:creator>
      <dc:date>2022-04-02T06:19:20Z</dc:date>
    </item>
    <item>
      <title>Re: stats or tstats latest not working on array fields</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Why-are-stats-or-tstats-latest-not-working-on-array-fields/m-p/592441#M206199</link>
      <description>&lt;P&gt;In my case, the fields are coming from different kinds of events and I am compiling them together. So can’t do head 1 etc as the latest of one field maybe in an older event, while latest of &amp;nbsp;another may be in the latest event. Etc.&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;Thanks for the suggestion though. Definitely useful to do when all the fields are in all events that i am looking at.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 05 Apr 2022 13:57:54 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Why-are-stats-or-tstats-latest-not-working-on-array-fields/m-p/592441#M206199</guid>
      <dc:creator>shikhanshua</dc:creator>
      <dc:date>2022-04-05T13:57:54Z</dc:date>
    </item>
  </channel>
</rss>

