Splunk Search

How to see only earliest and latest values in a field.

franciscof
Explorer

I'm having an issue because I need to show in a report only the first ticket received by an agent and the latest one, so all the other tickets in the middle I have to leave them behind. Here is the evidence:
alt text

Of all the tickets assigned to user1 or user2, how can I capture only the oldest and newest one?

Thanks in advance

Labels (3)
Tags (1)
0 Karma
1 Solution

gcusello
SplunkTrust
SplunkTrust

Hi @franciscof,
did you tried using earliest and latest in stats?

index=your_index ticket_arrival=*
| eval ticket_arrival=strptime(ticket_arrival,"%d/%m/%y - %H:%M:%S"")
| stats earliest(ticket_arrival) AS earliest latest(ticket_arrival) AS latest BY user
| eval earliest=strftime(earliest,"%d/%m/%y - %H:%M:%S""), latest=strftime(latest,"%d/%m/%y - %H:%M:%S"")

Please check the format of the ticket_arrival field, because I'm not sure about the spaces before hours.
Ciao.
Giuseppe

View solution in original post

manjunathmeti
Champion

Do this:

| eval ticket_arrival = strptime(replace(ticket_arrival, "\s", ""), "%d/%m/%y-%H:%M:%S") | stats min(ticket_arrival) as min_time, max(ticket_arrival) as max_time by user | eval min_time=strftime(min_time, "%d/%m/%y-%H:%M:%S"), max_time=strftime(max_time, "%d/%m/%y-%H:%M:%S")
0 Karma

franciscof
Explorer

alt text

any clues why this is happening?

0 Karma

woodcock
Esteemed Legend

The basic answer is to use earliest() and latest() but you can also use first() and last() just be aware that the former pair leverages the _time field (which may/not be present and may/not be correct) whereas the latter leverages the order of the data which you can control by sorting it. Both honor the BY section of stats.

0 Karma

to4kawa
Ultra Champion
| makeresults
| eval _raw="user,ticket_arrival
user1,02/03/20 - 12:43:47
user1,02/03/20 - 12:44:40
user1,03/03/20 - 16:05:08
user1,04/03/20 - 15:39:37
user1,05/03/20 - 15:22:25
user2,04/03/20 - 18:03:13
user2,04/03/20 - 09:39:24
user2,04/03/20 - 10:32:35
user2,04/03/20 - 15:05:25
user2,05/03/20 - 10:46:35
user2,05/03/20 - 10:47:10"
| multikv forceheader=1
| table user,ticket_arrival
| rename COMMENT as "this is sample your stats output, from here, the logic"
| eval _time=strptime(ticket_arrival,"%d/%m/%y - %T")
| eventstats min(_time) as e_time max(_time) as l_time by user
| where _time=e_time OR _time=l_time
| table user,ticket_arrival

Hi, If you provide your query, there is more easy query.

0 Karma

gcusello
SplunkTrust
SplunkTrust

Hi @franciscof,
did you tried using earliest and latest in stats?

index=your_index ticket_arrival=*
| eval ticket_arrival=strptime(ticket_arrival,"%d/%m/%y - %H:%M:%S"")
| stats earliest(ticket_arrival) AS earliest latest(ticket_arrival) AS latest BY user
| eval earliest=strftime(earliest,"%d/%m/%y - %H:%M:%S""), latest=strftime(latest,"%d/%m/%y - %H:%M:%S"")

Please check the format of the ticket_arrival field, because I'm not sure about the spaces before hours.
Ciao.
Giuseppe

franciscof
Explorer

Hi @gcusello , I did tried it and I think it didn't work and I can't figure out why. Below (in an answer) I'll show you what I got

0 Karma

franciscof
Explorer

Solved it! All it was needed was to replace stats earliest and latest with stats first and last!

Thank you for your help

0 Karma

gcusello
SplunkTrust
SplunkTrust

you're welcome!
Ciao and next time.
Giuseppe

0 Karma
Career Survey
First 500 qualified respondents will receive a $20 gift card! Tell us about your professional Splunk journey.
Get Updates on the Splunk Community!

Tech Talk Recap | Mastering Threat Hunting

Mastering Threat HuntingDive into the world of threat hunting, exploring the key differences between ...

Observability for AI Applications: Troubleshooting Latency

If you’re working with proprietary company data, you’re probably going to have a locally hosted LLM or many ...

Splunk AI Assistant for SPL vs. ChatGPT: Which One is Better?

In the age of AI, every tool promises to make our lives easier. From summarizing content to writing code, ...