<?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 search the duration between two events by field? in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/How-to-search-the-duration-between-two-events-by-field/m-p/244549#M72833</link>
    <description>&lt;P&gt;I currently have a log of json-formatted events that shows the changing value for several different IDs, like this:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;[{"fields": {"value": 1}, "measurement": "light", "tags": {"entity_id": "lightA", "domain": "light"}, "time": "2016-01-21 11:52:04+00:00"}]
[{"fields": {"value": 1}, "measurement": "light", "tags": {"entity_id": "lightB", "domain": "light"}, "time": "2016-01-21 11:52:09+00:00"}]
[{"fields": {"value": 1}, "measurement": "light", "tags": {"entity_id": "lightC", "domain": "light"}, "time": "2016-01-21 11:52:09+00:00"}]
[{"fields": {"value": 0}, "measurement": "light", "tags": {"entity_id": "lightB", "domain": "light"}, "time": "2016-01-21 12:04:54+00:00"}]
[{"fields": {"value": 0}, "measurement": "light", "tags": {"entity_id": "lightA", "domain": "light"}, "time": "2016-01-21 12:04:53+00:00"}]
[{"fields": {"value": 0}, "measurement": "light", "tags": {"entity_id": "lightC", "domain": "light"}, "time": "2016-01-21 12:04:59+00:00"}]
[{"fields": {"value": 1}, "measurement": "light", "tags": {"entity_id": "lightA", "domain": "light"}, "time": "2016-01-21 12:15:57+00:00"}]
[{"fields": {"value": 1}, "measurement": "light", "tags": {"entity_id": "lightB", "domain": "light"}, "time": "2016-01-21 12:15:58+00:00"}]`
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;I have the sourcetype defined so that I can pull the fields out into a table like this:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;index = * | table _time , entity_id , value

_time                 entity_id     value    
2016-01-21 06:52:04 lightA        1           
2016-01-21 06:52:09 lightB       1          
2016-01-21 06:52:10 lightC       1           
2016-01-21 07:04:54 lightB       0          
2016-01-21 07:04:54 lightA       0           
2016-01-21 07:04:59 lightC       0          
2016-01-21 07:15:57 lightA       1           
2016-01-21 07:15:58 lightB       1     
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;I want to create a table that includes the length of time each ID was that value (where the last one is considered to end at the current time), like this:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;_time                 entity_id    value      duration
2016-01-21 06:52:04 lightA      1           770
2016-01-21 06:52:09 lightB      1           765
2016-01-21 06:52:10 lightC      1           769
2016-01-21 07:04:54 lightB      0           664
2016-01-21 07:04:54 lightA      0           663
2016-01-21 07:04:59 lightC      0           9416
2016-01-21 07:15:57 lightA      1           8776
2016-01-21 07:15:58 lightB      1           8775
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;I've tried several approaches to using the transaction command for this (both on the initial search itself and on the table), but none of them are giving me the result I'm looking for, and I'm not sure if it's because I'm just misunderstanding how to use transaction, or if I'm using the wrong tool for the job.  I've also tried variations of streamstats, with an equal lack of success.  Can anyone point me in the right direction?&lt;/P&gt;</description>
    <pubDate>Thu, 21 Jan 2016 15:08:51 GMT</pubDate>
    <dc:creator>averyml</dc:creator>
    <dc:date>2016-01-21T15:08:51Z</dc:date>
    <item>
      <title>How to search the duration between two events by field?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-search-the-duration-between-two-events-by-field/m-p/244549#M72833</link>
      <description>&lt;P&gt;I currently have a log of json-formatted events that shows the changing value for several different IDs, like this:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;[{"fields": {"value": 1}, "measurement": "light", "tags": {"entity_id": "lightA", "domain": "light"}, "time": "2016-01-21 11:52:04+00:00"}]
[{"fields": {"value": 1}, "measurement": "light", "tags": {"entity_id": "lightB", "domain": "light"}, "time": "2016-01-21 11:52:09+00:00"}]
[{"fields": {"value": 1}, "measurement": "light", "tags": {"entity_id": "lightC", "domain": "light"}, "time": "2016-01-21 11:52:09+00:00"}]
[{"fields": {"value": 0}, "measurement": "light", "tags": {"entity_id": "lightB", "domain": "light"}, "time": "2016-01-21 12:04:54+00:00"}]
[{"fields": {"value": 0}, "measurement": "light", "tags": {"entity_id": "lightA", "domain": "light"}, "time": "2016-01-21 12:04:53+00:00"}]
[{"fields": {"value": 0}, "measurement": "light", "tags": {"entity_id": "lightC", "domain": "light"}, "time": "2016-01-21 12:04:59+00:00"}]
[{"fields": {"value": 1}, "measurement": "light", "tags": {"entity_id": "lightA", "domain": "light"}, "time": "2016-01-21 12:15:57+00:00"}]
[{"fields": {"value": 1}, "measurement": "light", "tags": {"entity_id": "lightB", "domain": "light"}, "time": "2016-01-21 12:15:58+00:00"}]`
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;I have the sourcetype defined so that I can pull the fields out into a table like this:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;index = * | table _time , entity_id , value

_time                 entity_id     value    
2016-01-21 06:52:04 lightA        1           
2016-01-21 06:52:09 lightB       1          
2016-01-21 06:52:10 lightC       1           
2016-01-21 07:04:54 lightB       0          
2016-01-21 07:04:54 lightA       0           
2016-01-21 07:04:59 lightC       0          
2016-01-21 07:15:57 lightA       1           
2016-01-21 07:15:58 lightB       1     
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;I want to create a table that includes the length of time each ID was that value (where the last one is considered to end at the current time), like this:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;_time                 entity_id    value      duration
2016-01-21 06:52:04 lightA      1           770
2016-01-21 06:52:09 lightB      1           765
2016-01-21 06:52:10 lightC      1           769
2016-01-21 07:04:54 lightB      0           664
2016-01-21 07:04:54 lightA      0           663
2016-01-21 07:04:59 lightC      0           9416
2016-01-21 07:15:57 lightA      1           8776
2016-01-21 07:15:58 lightB      1           8775
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;I've tried several approaches to using the transaction command for this (both on the initial search itself and on the table), but none of them are giving me the result I'm looking for, and I'm not sure if it's because I'm just misunderstanding how to use transaction, or if I'm using the wrong tool for the job.  I've also tried variations of streamstats, with an equal lack of success.  Can anyone point me in the right direction?&lt;/P&gt;</description>
      <pubDate>Thu, 21 Jan 2016 15:08:51 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-search-the-duration-between-two-events-by-field/m-p/244549#M72833</guid>
      <dc:creator>averyml</dc:creator>
      <dc:date>2016-01-21T15:08:51Z</dc:date>
    </item>
    <item>
      <title>Re: How to search the duration between two events by field?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-search-the-duration-between-two-events-by-field/m-p/244550#M72834</link>
      <description>&lt;P&gt;Give this a try&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;index=I_suggest_You_Specify_IndexName_Here | table _time , entity_id , value | sort entity_id,_time | streamstats window=1 current=f values(value) as prev values(_time) as prev_time by entity_Id | where isnotnull(prev) | eval duration=_time-prev_time | table entity_id prev duration | rename prev as value
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 21 Jan 2016 17:26:20 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-search-the-duration-between-two-events-by-field/m-p/244550#M72834</guid>
      <dc:creator>somesoni2</dc:creator>
      <dc:date>2016-01-21T17:26:20Z</dc:date>
    </item>
    <item>
      <title>Re: How to search the duration between two events by field?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-search-the-duration-between-two-events-by-field/m-p/244551#M72835</link>
      <description>&lt;P&gt;&lt;STRONG&gt;transaction&lt;/STRONG&gt; may be the tool. but a &lt;STRONG&gt;streamstats&lt;/STRONG&gt; can also be less expensive.&lt;/P&gt;

&lt;P&gt;see &lt;A href="http://docs.splunk.com/Documentation/Splunk/6.3.2/SearchReference/Transaction"&gt;http://docs.splunk.com/Documentation/Splunk/6.3.2/SearchReference/Transaction&lt;/A&gt;&lt;/P&gt;

&lt;P&gt;simple example : (you may want to add some rules to specify what is a first ecnt&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;    &amp;lt;mysearch&amp;gt; | transaction source host mycommonfield maxevents=2 | table _time duration mycommonfield _raw
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;or &lt;A href="http://docs.splunk.com/Documentation/Splunk/6.3.2/SearchReference/Streamstats"&gt;http://docs.splunk.com/Documentation/Splunk/6.3.2/SearchReference/Streamstats&lt;/A&gt;&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;    &amp;lt;mysearch&amp;gt; | streamstats range(_time) as Duration window=2
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 21 Jan 2016 19:12:34 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-search-the-duration-between-two-events-by-field/m-p/244551#M72835</guid>
      <dc:creator>yannK</dc:creator>
      <dc:date>2016-01-21T19:12:34Z</dc:date>
    </item>
    <item>
      <title>Re: How to search the duration between two events by field?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-search-the-duration-between-two-events-by-field/m-p/244552#M72836</link>
      <description>&lt;P&gt;This is so very very close (I did have to add prev_time as a field to the table, but that was trivial) - but using this methodology, I can't figure out how to show the last transition.  What I want is a kind of &lt;CODE&gt;eval duration=if(,_time-prev_time, now()-_time)&lt;/CODE&gt; - except that doesn't seem to work in this case.  I have 12 events, but the table ends up showing 9.&lt;/P&gt;</description>
      <pubDate>Tue, 29 Sep 2020 08:31:40 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-search-the-duration-between-two-events-by-field/m-p/244552#M72836</guid>
      <dc:creator>averyml</dc:creator>
      <dc:date>2020-09-29T08:31:40Z</dc:date>
    </item>
    <item>
      <title>Re: How to search the duration between two events by field?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-search-the-duration-between-two-events-by-field/m-p/244553#M72837</link>
      <description>&lt;P&gt;&lt;CODE&gt;* |streamstats range(_time) as Duration window=2&lt;/CODE&gt; gives me the time between each event, but not the time between each event, per entity_id.  I had tried &lt;CODE&gt;* |streamstats range(_time)  by entity_id as Duration window=2&lt;/CODE&gt; before, and I thought it didn't work because there was no resulting Duration field, but I just realized that &lt;CODE&gt;* |streamstats range(_time)&lt;/CODE&gt; &lt;EM&gt;does&lt;/EM&gt; work, I just can't use the "by" clause with the "as" clause or the window.  This seems to give me the difference between the time of each event and the last event of that entity_id.  Another question/answer here makes it sound like &lt;CODE&gt;global=f&lt;/CODE&gt; is what I want, but this:&lt;/P&gt;

&lt;P&gt;` |sort -_time| streamstats range(_time) by entity_id| table _time, entity_id, value, range(_time)&lt;/P&gt;

&lt;P&gt;and this:&lt;/P&gt;

&lt;P&gt;&lt;CODE&gt;|sort -_time| streamstats global=f range(_time) by entity_id| table _time, entity_id, value, range(_time)&lt;/CODE&gt;&lt;/P&gt;

&lt;P&gt;give me the same result.  Is there something wrong with my syntax?&lt;/P&gt;</description>
      <pubDate>Tue, 29 Sep 2020 08:31:43 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-search-the-duration-between-two-events-by-field/m-p/244553#M72837</guid>
      <dc:creator>averyml</dc:creator>
      <dc:date>2020-09-29T08:31:43Z</dc:date>
    </item>
    <item>
      <title>Re: How to search the duration between two events by field?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-search-the-duration-between-two-events-by-field/m-p/244554#M72838</link>
      <description>&lt;P&gt;oh, I need both global=f &lt;EM&gt;and&lt;/EM&gt; window=2.  &lt;/P&gt;

&lt;P&gt;this works:&lt;BR /&gt;
&lt;CODE&gt;*|sort -_time| streamstats range(_time) by entity_id window=2 global=f| table _time, entity_id, value, range(_time)&lt;/CODE&gt;&lt;/P&gt;

&lt;P&gt;or, with the duration adjustment:&lt;BR /&gt;
&lt;CODE&gt;*|sort -_time| streamstats range(_time) by entity_id window=2 global=f| table _time, entity_id, value, range(_time)|rename range(_time) as duration|eval duration=if(duration==0,now()-_time,duration)&lt;/CODE&gt;&lt;/P&gt;

&lt;P&gt;Thank you!&lt;/P&gt;</description>
      <pubDate>Thu, 21 Jan 2016 20:33:20 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-search-the-duration-between-two-events-by-field/m-p/244554#M72838</guid>
      <dc:creator>averyml</dc:creator>
      <dc:date>2016-01-21T20:33:20Z</dc:date>
    </item>
  </channel>
</rss>

