<?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 write a search to display the end date of field values and the time difference? in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/How-to-write-a-search-to-display-the-end-date-of-field-values/m-p/218412#M64180</link>
    <description>&lt;P&gt;Thank you! I have an syntax error near to line 2:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;Error in 'stats' command: The argument 'Events' is invalid.
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Any idea?&lt;/P&gt;</description>
    <pubDate>Sat, 12 Nov 2016 17:02:18 GMT</pubDate>
    <dc:creator>changux</dc:creator>
    <dc:date>2016-11-12T17:02:18Z</dc:date>
    <item>
      <title>How to write a search to display the end date of field values and the time difference?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-write-a-search-to-display-the-end-date-of-field-values/m-p/218408#M64176</link>
      <description>&lt;P&gt;Hi all.&lt;/P&gt;

&lt;P&gt;I have a sourcetype with &lt;CODE&gt;PENDING&lt;/CODE&gt; orders in a field: &lt;CODE&gt;ORDERID&lt;/CODE&gt;. In other sourcetype i have &lt;CODE&gt;ANSWERED&lt;/CODE&gt; orders with the same &lt;CODE&gt;ORDERID&lt;/CODE&gt;. I need to show in a table the ORDERS PENDING that were ANSWERED with the respective date (the same index time in both cases).&lt;BR /&gt;
If i have the ORDERID in the ANSWERED sourcetype should be considered as ANSWERED (no special field with statuses). &lt;/P&gt;

&lt;P&gt;I can do a join to know common ORDERIDs, but i don't know how display the date when the ORDERID is on ANSWERED sourcetype.&lt;/P&gt;

&lt;P&gt;&lt;CODE&gt;sourcetype=pending ORDERID=*&lt;/CODE&gt; returns orders pending.&lt;BR /&gt;
&lt;CODE&gt;sourcetype=answered ORDERID=*&lt;/CODE&gt; return answered.&lt;/P&gt;

&lt;P&gt;I can do:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;sourcetype=pending | join ORDERID [search sourcetype=answered]
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;I need to show something like:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;ORDERID   |  Date published     |  Date closed         |  Total time to fix 
4424424   | 10/1/16 12:00:00    |  10/2/16 12:00:00    | 24:00:00
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Any suggestion?&lt;/P&gt;</description>
      <pubDate>Sat, 12 Nov 2016 03:37:55 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-write-a-search-to-display-the-end-date-of-field-values/m-p/218408#M64176</guid>
      <dc:creator>changux</dc:creator>
      <dc:date>2016-11-12T03:37:55Z</dc:date>
    </item>
    <item>
      <title>Re: How to write a search to display the end date of field values and the time difference?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-write-a-search-to-display-the-end-date-of-field-values/m-p/218409#M64177</link>
      <description>&lt;P&gt;Even though &lt;CODE&gt;transaction&lt;/CODE&gt; is considered one to be avoided but this scenario seems to have all the makings to use one. Since assumption is that:&lt;/P&gt;

&lt;UL&gt;
&lt;LI&gt; "Date published" will always be earlier than "Date Closed"&lt;/LI&gt;
&lt;LI&gt; "Date published"  is the first event and "Date Closed" is the last event in the set of events for a particular ORDERID.&lt;/LI&gt;
&lt;LI&gt; None of the events will be closed before they open.&lt;/LI&gt;
&lt;LI&gt; No ORDERID will be published or closed twice.&lt;/LI&gt;
&lt;/UL&gt;

&lt;P&gt;Hence based on above please try below query with &lt;CODE&gt;transaction ORDERID&lt;/CODE&gt; which will provide &lt;CODE&gt;_time&lt;/CODE&gt; which is the first event's time and can be used as &lt;CODE&gt;publish time&lt;/CODE&gt;, &lt;CODE&gt;duration&lt;/CODE&gt; to compute the &lt;CODE&gt;closed time&lt;/CODE&gt; and the &lt;CODE&gt;eventcount&lt;/CODE&gt; to check we capture events which have both ORDERID being open and closed:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;index=* sourcetype=pending OR sourcetype=answered ORDERID=*
| transaction ORDERID
| eval datePublished=_time
| eval dateClosed=_time+duration
| eval totalFixTime=duration
| eval datePublished=strftime(datePublished,"%Y/%m/%d %H:%M:%S")  
| eval dateClosed=strftime(dateClosed,"%Y/%m/%d %H:%M:%S")
| eval totalFixTime=totalFixTime/(60*60*24)
| table ORDERID, datePublished, dateClosed, totalFixTime, eventcount | where eventcount&amp;gt;=2 | fields -eventcount
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;&lt;STRONG&gt;Updated to human readable time&lt;/STRONG&gt; as per comments&lt;/P&gt;</description>
      <pubDate>Sat, 12 Nov 2016 04:41:37 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-write-a-search-to-display-the-end-date-of-field-values/m-p/218409#M64177</guid>
      <dc:creator>gokadroid</dc:creator>
      <dc:date>2016-11-12T04:41:37Z</dc:date>
    </item>
    <item>
      <title>Re: How to write a search to display the end date of field values and the time difference?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-write-a-search-to-display-the-end-date-of-field-values/m-p/218410#M64178</link>
      <description>&lt;P&gt;Like gokadroid has mentioned transaction will be an expensive command to run for the following use case where the events may be from longer duration (several days). Transaction will run slow and might even drop orphaned records. Refer to following documentation on when to choose which event correlation techniques. Transaction would have been suitable only if you were looking for specific ORDERID across various sourcetype and events.&lt;/P&gt;

&lt;P&gt;&lt;A href="http://docs.splunk.com/Documentation/Splunk/latest/Search/Abouteventcorrelation"&gt;http://docs.splunk.com/Documentation/Splunk/latest/Search/Abouteventcorrelation&lt;/A&gt;&lt;/P&gt;

&lt;P&gt;Kindly try the following stats command instead which should hopefully be faster.&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;index=&amp;lt;YOUR INDEX NAME HERE&amp;gt; (sourcetype="pending" OR sourcetype="answered") orderid=*
 | stats values(sourcetype) as Events count as EventCount min(_time) as DateTimePublished max(_time) as DateTimeAnswered by orderid
 | eval closureDurationInDays= round((DateTimeAnswered-DateTimePublished)/(60*60*24),2)
 | eval DateTimePublished=strftime(DateTimePublished,"%Y/%m/%d %H:%M:%S")
 | eval DateTimeAnswered=strftime(DateTimeAnswered,"%Y/%m/%d %H:%M:%S")
 | search  EventCount&amp;gt;=2 AND Events="pending" AND Events="answered"
 | table orderid, Events, EventCount, DateTimePublished, DateTimeAnswered, closureDurationInDays
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Some of the query optimization facts:&lt;BR /&gt;
1) Define sourcetypes and index in first filter.&lt;BR /&gt;
2) Add &lt;STRONG&gt;orderid="*"&lt;/STRONG&gt; in the first filter to remove any unwanted events upfront.&lt;BR /&gt;
3) Since you do not have separate status field perform &lt;STRONG&gt;values(sourcetype)&lt;/STRONG&gt; stats aggregation to get distinct values of sourcetypes as &lt;STRONG&gt;Events&lt;/STRONG&gt;.&lt;BR /&gt;
4) Use search EventCount&amp;gt;=2 instead of where clause. Also search for both Event="published" and Event="answered". This is just for special scenario like what if there were two queries published for same order id and no answers etc. (Might not be in present in your use case or might be even more complicated).&lt;BR /&gt;
5) You can play around with Event and EventCount fields to capture other scenarios like Ticket pending but not answered etc. Compare DateTimePublished with &lt;STRONG&gt;now()&lt;/STRONG&gt; to get whether they have remained opened for longer than expected SLA.&lt;/P&gt;

&lt;P&gt;Let me know if you need any other info regarding the same.&lt;/P&gt;</description>
      <pubDate>Sat, 12 Nov 2016 11:06:12 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-write-a-search-to-display-the-end-date-of-field-values/m-p/218410#M64178</guid>
      <dc:creator>niketn</dc:creator>
      <dc:date>2016-11-12T11:06:12Z</dc:date>
    </item>
    <item>
      <title>Re: How to write a search to display the end date of field values and the time difference?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-write-a-search-to-display-the-end-date-of-field-values/m-p/218411#M64179</link>
      <description>&lt;P&gt;Great, thanks. Dates and fixtime are in EPOC or seconds, how i can convert to human?&lt;/P&gt;</description>
      <pubDate>Sat, 12 Nov 2016 16:56:23 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-write-a-search-to-display-the-end-date-of-field-values/m-p/218411#M64179</guid>
      <dc:creator>changux</dc:creator>
      <dc:date>2016-11-12T16:56:23Z</dc:date>
    </item>
    <item>
      <title>Re: How to write a search to display the end date of field values and the time difference?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-write-a-search-to-display-the-end-date-of-field-values/m-p/218412#M64180</link>
      <description>&lt;P&gt;Thank you! I have an syntax error near to line 2:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;Error in 'stats' command: The argument 'Events' is invalid.
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Any idea?&lt;/P&gt;</description>
      <pubDate>Sat, 12 Nov 2016 17:02:18 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-write-a-search-to-display-the-end-date-of-field-values/m-p/218412#M64180</guid>
      <dc:creator>changux</dc:creator>
      <dc:date>2016-11-12T17:02:18Z</dc:date>
    </item>
    <item>
      <title>Re: How to write a search to display the end date of field values and the time difference?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-write-a-search-to-display-the-end-date-of-field-values/m-p/218413#M64181</link>
      <description>&lt;P&gt;Hi. Fixed with &lt;CODE&gt;as Events&lt;/CODE&gt;.&lt;BR /&gt;
Now, my problem is that &lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;... | eval DateTimePublished=strftime(publishedTime,"%Y/%m/%d %H:%M:%S")   | eval DateTimeAnswered=strftime(closedTime,"%Y/%m/%d %H:%M:%S") 
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Doesn't format the dates. Other suggestion?&lt;/P&gt;</description>
      <pubDate>Sat, 12 Nov 2016 17:13:04 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-write-a-search-to-display-the-end-date-of-field-values/m-p/218413#M64181</guid>
      <dc:creator>changux</dc:creator>
      <dc:date>2016-11-12T17:13:04Z</dc:date>
    </item>
    <item>
      <title>Re: How to write a search to display the end date of field values and the time difference?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-write-a-search-to-display-the-end-date-of-field-values/m-p/218414#M64182</link>
      <description>&lt;P&gt;Again, fixed:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| eval DateTimePublished=strftime(DateTimePublished,"%Y/%m/%d %H:%M:%S")   | eval DateTimeAnswered=strftime(DateTimeAnswered,"%Y/%m/%d %H:%M:%S")
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;About your point #5, how i can show Tickets pending (no answered)?&lt;/P&gt;

&lt;P&gt;Thanks again!&lt;/P&gt;</description>
      <pubDate>Sat, 12 Nov 2016 17:21:28 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-write-a-search-to-display-the-end-date-of-field-values/m-p/218414#M64182</guid>
      <dc:creator>changux</dc:creator>
      <dc:date>2016-11-12T17:21:28Z</dc:date>
    </item>
    <item>
      <title>Re: How to write a search to display the end date of field values and the time difference?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-write-a-search-to-display-the-end-date-of-field-values/m-p/218415#M64183</link>
      <description>&lt;P&gt;Solved! Thanks!&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;... | eval datePublished=_time  | eval datePublished=strftime(datePublished,"%Y/%m/%d %H:%M:%S")  | eval dateClosed=_time+duration | eval dateClosed=strftime(dateClosed,"%Y/%m/%d %H:%M:%S")   | eval totalFixTime=duration | eval totalFixTime=totalFixTime/(60*60*24) | table ORDERID, datePublished, dateClosed, totalFixTime, eventcount | where eventcount&amp;gt;=2 | fields - eventcount
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 12 Nov 2016 17:35:55 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-write-a-search-to-display-the-end-date-of-field-values/m-p/218415#M64183</guid>
      <dc:creator>changux</dc:creator>
      <dc:date>2016-11-12T17:35:55Z</dc:date>
    </item>
    <item>
      <title>Re: How to write a search to display the end date of field values and the time difference?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-write-a-search-to-display-the-end-date-of-field-values/m-p/218416#M64184</link>
      <description>&lt;P&gt;@changux... Sorry had made some last minute changes to post trying to reduce number or fields and make more sense out of field names, so there were some corrections I missed out. I have corrected my post, also changed EventCount &lt;STRONG&gt;&amp;gt;=&lt;/STRONG&gt;2, in case there are multiple published and multiple answered events for the same orderid. &lt;/P&gt;

&lt;P&gt;For Pending tickets you need only those tickets which are Published but not answered, so change the search as following (condition &amp;gt;=1 in place just in case there is possibility of more than one pending Events for same orderid):&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| search  EventCount&amp;gt;=1 AND Events="pending" AND Events!="answered"
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 12 Nov 2016 18:48:46 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-write-a-search-to-display-the-end-date-of-field-values/m-p/218416#M64184</guid>
      <dc:creator>niketn</dc:creator>
      <dc:date>2016-11-12T18:48:46Z</dc:date>
    </item>
  </channel>
</rss>

