<?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 calculate time difference between two identical events in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/How-to-calculate-time-difference-between-two-identical-events/m-p/484941#M135745</link>
    <description>&lt;P&gt;Hi Peter with many e,&lt;/P&gt;

&lt;P&gt;This seems to be the same as :&lt;BR /&gt;
&lt;A href="https://answers.splunk.com/answers/772916/time-difference-by-grouping-identical-events.html?childToView=773813#answer-773813"&gt;https://answers.splunk.com/answers/772916/time-difference-by-grouping-identical-events.html?childToView=773813#answer-773813&lt;/A&gt;&lt;/P&gt;

&lt;P&gt;You should be able to use the following command as well:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt; source="*.log"
 | rex field=_raw "message (?&amp;lt;product&amp;gt;.*?):(?&amp;lt;Currency&amp;gt;.*?):(?&amp;lt;type&amp;gt;.*)"
 |stats earliest(_time) as StartTime latest(StartTime) as EndTime by product,Currency,type
 | eval duration= tostring(EndTime-StartTime,"duration")
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Let me know how that works out for you.&lt;/P&gt;

&lt;P&gt;Cheers,&lt;BR /&gt;
David&lt;/P&gt;</description>
    <pubDate>Mon, 23 Sep 2019 07:27:20 GMT</pubDate>
    <dc:creator>DavidHourani</dc:creator>
    <dc:date>2019-09-23T07:27:20Z</dc:date>
    <item>
      <title>How to calculate time difference between two identical events</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-calculate-time-difference-between-two-identical-events/m-p/484940#M135744</link>
      <description>&lt;H2&gt;I have the following events&lt;/H2&gt;

&lt;P&gt;**2019-09-20 01:39:25 INFO  Listener processing event with message metal:AUD:ADJ&lt;BR /&gt;
2019-09-19 23:58:27 INFO  Listener processing event with message metal:USD:ADJ&lt;BR /&gt;
2019-09-19 23:58:20 INFO  Listener processing event with message metal:USD:ADJ&lt;BR /&gt;
2019-09-19 23:19:30 INFO  Listener processing event with message metal:AUD:ADJ&lt;BR /&gt;
2019-09-21 04:15:55 INFO  Listener processing event with message metal:AUD:ADJ&lt;BR /&gt;
2019-09-20 21:12:11 INFO  Listener processing event with message metal:USD:ADJ&lt;BR /&gt;
2019-09-21 04:15:45 INFO  Listener processing event with message metal:AUD:ADJ&lt;/P&gt;

&lt;H2&gt;2019-09-20 21:12:17 INFO  Listener processing event with message metal:USD:ADJ**&lt;/H2&gt;

&lt;P&gt;These events are exactly same in pattern, and they only differ in timestamps (The events come in pairs, sometimes there are many pairs with the same key). The timestamps are startTime and endTime. I am trying to summarise these events, by grouping the pair into one row, and calculating the duration. &lt;/P&gt;

&lt;H2&gt;So the output will look like this.&lt;/H2&gt;

&lt;P&gt;**product | currency | type | startTime | endTime | duration&lt;BR /&gt;
metal | AUD | ADJ | 2019-09-20 01:39:25 | 2019-09-19 23:19:30 | 5&lt;BR /&gt;
                                  |2019-09-21 04:15:45 | 2019-09-21 04:15:55 | 10&lt;BR /&gt;
metal | USD | ADJ | 2019-09-19 23:58:20 | 2019-09-19 23:58:27 | 7&lt;/P&gt;

&lt;H2&gt;                                  | 2019-09-20 21:12:11 | 2019-09-20 21:12:17 | 6**&lt;/H2&gt;

&lt;P&gt;I have began my search as follows:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;source="*.log"
| rex field=_raw "message (?&amp;lt;product&amp;gt;.*?):(?&amp;lt;Currency&amp;gt;.*?):(?&amp;lt;type&amp;gt;.*)"

| table Config Intent Currency RunType AccountingDate _time
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;I have tried both the transaction and stats function, yet in vain. Maybe I am not using them correctly.&lt;/P&gt;

&lt;P&gt;Thanks to David, I made some progress by using the first/last(earliest/latest) function. However they only capture the first and last event. Is there a way to group them by the same key, and aggregate the values like my output as above. &lt;/P&gt;

&lt;P&gt;Is there anyone who can give me any advice on this issue? Any help will be highly appreciated. &lt;/P&gt;</description>
      <pubDate>Mon, 23 Sep 2019 06:00:21 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-calculate-time-difference-between-two-identical-events/m-p/484940#M135744</guid>
      <dc:creator>peeeeeeeeeeter</dc:creator>
      <dc:date>2019-09-23T06:00:21Z</dc:date>
    </item>
    <item>
      <title>Re: How to calculate time difference between two identical events</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-calculate-time-difference-between-two-identical-events/m-p/484941#M135745</link>
      <description>&lt;P&gt;Hi Peter with many e,&lt;/P&gt;

&lt;P&gt;This seems to be the same as :&lt;BR /&gt;
&lt;A href="https://answers.splunk.com/answers/772916/time-difference-by-grouping-identical-events.html?childToView=773813#answer-773813"&gt;https://answers.splunk.com/answers/772916/time-difference-by-grouping-identical-events.html?childToView=773813#answer-773813&lt;/A&gt;&lt;/P&gt;

&lt;P&gt;You should be able to use the following command as well:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt; source="*.log"
 | rex field=_raw "message (?&amp;lt;product&amp;gt;.*?):(?&amp;lt;Currency&amp;gt;.*?):(?&amp;lt;type&amp;gt;.*)"
 |stats earliest(_time) as StartTime latest(StartTime) as EndTime by product,Currency,type
 | eval duration= tostring(EndTime-StartTime,"duration")
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Let me know how that works out for you.&lt;/P&gt;

&lt;P&gt;Cheers,&lt;BR /&gt;
David&lt;/P&gt;</description>
      <pubDate>Mon, 23 Sep 2019 07:27:20 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-calculate-time-difference-between-two-identical-events/m-p/484941#M135745</guid>
      <dc:creator>DavidHourani</dc:creator>
      <dc:date>2019-09-23T07:27:20Z</dc:date>
    </item>
    <item>
      <title>Re: How to calculate time difference between two identical events</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-calculate-time-difference-between-two-identical-events/m-p/484942#M135746</link>
      <description>&lt;P&gt;Good answer, but you'd really want to use &lt;CODE&gt;earliest()&lt;/CODE&gt; and &lt;CODE&gt;latest()&lt;/CODE&gt; in case the events are not sorted chronologically (or in the reverse direction).&lt;/P&gt;

&lt;P&gt;&lt;A href="https://splunkbase.splunk.com/app/1603/"&gt;https://splunkbase.splunk.com/app/1603/&lt;/A&gt;&lt;/P&gt;

&lt;P&gt;Keep up the great work @DavidHourani &lt;span class="lia-unicode-emoji" title=":winking_face:"&gt;😉&lt;/span&gt; you always give good answers.&lt;/P&gt;</description>
      <pubDate>Mon, 23 Sep 2019 23:01:24 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-calculate-time-difference-between-two-identical-events/m-p/484942#M135746</guid>
      <dc:creator>jacobpevans</dc:creator>
      <dc:date>2019-09-23T23:01:24Z</dc:date>
    </item>
    <item>
      <title>Re: How to calculate time difference between two identical events</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-calculate-time-difference-between-two-identical-events/m-p/484943#M135747</link>
      <description>&lt;P&gt;Hi David, thank you for the answer and I apologise for repeated question. It was the first time I used this site... &lt;/P&gt;

&lt;P&gt;The query is great, it gives me the correct format. However, the first and last function only capture the first and last event with the same key. In my logs, there are more than one pair of such events, they normally come next to each other. &lt;/P&gt;</description>
      <pubDate>Mon, 23 Sep 2019 23:46:23 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-calculate-time-difference-between-two-identical-events/m-p/484943#M135747</guid>
      <dc:creator>peeeeeeeeeeter</dc:creator>
      <dc:date>2019-09-23T23:46:23Z</dc:date>
    </item>
    <item>
      <title>Re: How to calculate time difference between two identical events</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-calculate-time-difference-between-two-identical-events/m-p/484944#M135748</link>
      <description>&lt;P&gt;The two options that come to mind to me are using a &lt;CODE&gt;stats&lt;/CODE&gt; (building off of David's answer) with a _time delimiter, or using &lt;CODE&gt;transaction&lt;/CODE&gt; (&lt;A href="https://docs.splunk.com/Documentation/Splunk/latest/SearchReference/Transaction"&gt;https://docs.splunk.com/Documentation/Splunk/latest/SearchReference/Transaction&lt;/A&gt;)&lt;/P&gt;

&lt;P&gt;Normally, stats is preferred, but that might not work out for you. Do you know the maximum time between events? Transaction performs a similar thing to David's suggestion, but you can specify a max gap time between events so that you get multiple matches even when the events are otherwise identical (besides time). To use stats, you need to add &lt;CODE&gt;bin&lt;/CODE&gt; (&lt;A href="https://docs.splunk.com/Documentation/Splunk/latest/SearchReference/Bin"&gt;https://docs.splunk.com/Documentation/Splunk/latest/SearchReference/Bin&lt;/A&gt;) e.g. &lt;CODE&gt;| bin _time span=5m | stats earliest(_time) as StartTime latest(StartTime) as EndTime by product,Currency,type&lt;/CODE&gt;&lt;/P&gt;

&lt;P&gt;Transaction also gives you a built-in duration field so no need to calculate yourself.&lt;/P&gt;</description>
      <pubDate>Tue, 24 Sep 2019 03:14:31 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-calculate-time-difference-between-two-identical-events/m-p/484944#M135748</guid>
      <dc:creator>jacobpevans</dc:creator>
      <dc:date>2019-09-24T03:14:31Z</dc:date>
    </item>
    <item>
      <title>Re: How to calculate time difference between two identical events</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-calculate-time-difference-between-two-identical-events/m-p/484945#M135749</link>
      <description>&lt;P&gt;@jacobevans Thanks man! And you're right, I've edited the answer ^^&lt;/P&gt;

&lt;P&gt;@peeeeeeeeeeter for some reason I thought that those fields would build a sort of serial number, but I guess I missed &lt;CODE&gt;many pairs with the same key&lt;/CODE&gt;. So I see two ways to do this :&lt;BR /&gt;
1- Make a new field using &lt;CODE&gt;streamstats&lt;/CODE&gt; to include the latest time, then use that field for the duration.&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;       source="*.log"
      | rex field=_raw "message (?&amp;lt;product&amp;gt;.*?):(?&amp;lt;Currency&amp;gt;.*?):(?&amp;lt;type&amp;gt;.*)"
      | streamstats current=f window=1 latest(StartTime) as EndTime earliest(_time) as StartTime by product,Currency,type
      | eval duration= tostring(EndTime-StartTime,"duration")
      | table product Currency type EndTime StartTime duration
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;This might not do the trick though because there is no way to tell which event is a start and which is an end and that means that we will get the duration between any two consecutive events with the same keys.&lt;/P&gt;

&lt;P&gt;2- Create a serial number using &lt;CODE&gt;streamstats&lt;/CODE&gt; or &lt;CODE&gt;eval&lt;/CODE&gt; then use that serialnumber to build the transaction either using &lt;CODE&gt;stats&lt;/CODE&gt; or &lt;CODE&gt;transaction&lt;/CODE&gt; . This is the right way to do it, but I don't see how we can do that since all the events look the same.&lt;/P&gt;

&lt;P&gt;Hope this helps.&lt;/P&gt;</description>
      <pubDate>Tue, 24 Sep 2019 03:20:06 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-calculate-time-difference-between-two-identical-events/m-p/484945#M135749</guid>
      <dc:creator>DavidHourani</dc:creator>
      <dc:date>2019-09-24T03:20:06Z</dc:date>
    </item>
  </channel>
</rss>

