Splunk Search

Difference between time fields in different events based off of the value of another field

fmatera
Explorer

I would like to find the difference of time where based on the value of field "disposion", I choose the time value form another field.
for start idisposion="ANSWERED", I use field answer
for stop disposion="NO ANSWER", I use field cr-date

I have tried this:

based search | eval start =
if(disposion="ANSWERED",strptime(answer,"%Y-%m-%d
%H:%M:%S"),0) |eval stop =
if(disposion="NO
ANSWER",strptime(cr_date,"%Y-%m-%d
%H:%M:%S"),0) |eval results =
stop-start |table results

And this:

base search | dedup disposion | eval
start = strptime(answer,"%Y-%m-%d
%H:%M:%S") | where
disposion="ANSWERED" |eval stop =
strptime(cr_date,"%Y-%m-%d %H:%M:%S")
|where disposion="NO ANSWER" |eval
results = stop-start |table results

Any ideas? Thanks

Tags (2)
0 Karma
1 Solution

kamlesh_vaghela
SplunkTrust
SplunkTrust

@fmatera

Can you please try below search?

based search | eval start = if(disposion="ANSWERED",strptime(answer,"%Y-%m-%d %H:%M:%S"),NULL) 
| eval stop = if(disposion="NO ANSWER",strptime(cr_date,"%Y-%m-%d %H:%M:%S"),NULL) 
| stats values(start) as start values(stop) as stop 
| eval results = stop - start 
| table results

If you have any unique id in event, like UNIQUE_ID then

based search | eval start = if(disposion="ANSWERED",strptime(answer,"%Y-%m-%d %H:%M:%S"),NULL) 
| eval stop = if(disposion="NO ANSWER",strptime(cr_date,"%Y-%m-%d %H:%M:%S"),NULL) 
| stats values(start) as start values(stop) as stop by UNIQUE_ID  
| eval results = stop - start 
| table UNIQUE_ID  results

My Sample Search:

| makeresults 
| eval disposion="ANSWERED",answer="2019-01-01 00:00:00" 
| append 
    [| makeresults 
    | eval disposion="NO ANSWER",cr_date="2019-01-01 00:05:00" ] 
| eval start = if(disposion="ANSWERED",strptime(answer,"%Y-%m-%d %H:%M:%S"),NULL) 
| eval stop = if(disposion="NO ANSWER",strptime(cr_date,"%Y-%m-%d %H:%M:%S"),NULL) 
| stats values(start) as start values(stop) as stop 
| eval results = stop - start 
| table results

Thanks

View solution in original post

arkadyz1
Builder

It's not clear - do you want to have both start and stop calculated within the same event, or do you group your events somehow and then one of the events in the group should provide a 'start' while another yields 'stop'? Can you show at least a small extract of the relevant fields of your data and explain what you are trying to achieve?

0 Karma

kamlesh_vaghela
SplunkTrust
SplunkTrust

@fmatera

Can you please try below search?

based search | eval start = if(disposion="ANSWERED",strptime(answer,"%Y-%m-%d %H:%M:%S"),NULL) 
| eval stop = if(disposion="NO ANSWER",strptime(cr_date,"%Y-%m-%d %H:%M:%S"),NULL) 
| stats values(start) as start values(stop) as stop 
| eval results = stop - start 
| table results

If you have any unique id in event, like UNIQUE_ID then

based search | eval start = if(disposion="ANSWERED",strptime(answer,"%Y-%m-%d %H:%M:%S"),NULL) 
| eval stop = if(disposion="NO ANSWER",strptime(cr_date,"%Y-%m-%d %H:%M:%S"),NULL) 
| stats values(start) as start values(stop) as stop by UNIQUE_ID  
| eval results = stop - start 
| table UNIQUE_ID  results

My Sample Search:

| makeresults 
| eval disposion="ANSWERED",answer="2019-01-01 00:00:00" 
| append 
    [| makeresults 
    | eval disposion="NO ANSWER",cr_date="2019-01-01 00:05:00" ] 
| eval start = if(disposion="ANSWERED",strptime(answer,"%Y-%m-%d %H:%M:%S"),NULL) 
| eval stop = if(disposion="NO ANSWER",strptime(cr_date,"%Y-%m-%d %H:%M:%S"),NULL) 
| stats values(start) as start values(stop) as stop 
| eval results = stop - start 
| table results

Thanks

fmatera
Explorer

Thanks. Worked perfectly!

0 Karma

p_gurav
Champion

Can you please provide sample data?

0 Karma
Get Updates on the Splunk Community!

Stay Connected: Your Guide to May Tech Talks, Office Hours, and Webinars!

Take a look below to explore our upcoming Community Office Hours, Tech Talks, and Webinars this month. This ...

They're back! Join the SplunkTrust and MVP at .conf24

With our highly anticipated annual conference, .conf, comes the fez-wearers you can trust! The SplunkTrust, as ...

Enterprise Security Content Update (ESCU) | New Releases

Last month, the Splunk Threat Research Team had two releases of new security content via the Enterprise ...