Reporting

How to extract the smallest positive number from a multivalue field

andres91302
Communicator

Hello everyone I hope everyone is having a great day I have been bumping my head against the wall trying to select the smallest positive number from a multi-value field...

 

I have a SPL that looks like this:

| Stats values(numbers) as NUMBERS values(KEY) as KEY by client 

But the field NUMBERS may have multiple values per client and I want to select the smallest positive number and the respective KEY associated with it....

For instance I may have something like this

Client           NUMBERS.    KEY

NATALIE.             -7.               U

                                  8.                Y

                                   5.                M

From the last result I will be interested in having only:

Client           NUMBERS.    KEY

NATALIE.             5                  M

   Does anyone know how to achieve this? I will be so thankful if you guys can help me out thank you guys so much!

                                  

 

 

 

 

 

 

 

 

Labels (1)
0 Karma
1 Solution

bowesmana
SplunkTrust
SplunkTrust

Here's a solution that first looks for the minimum value (NUMBERS>=0) and then takes the Key from the KEY values

| makeresults
| eval Client="NATALIE", NUMBERS=split("-7,8,5",","), KEY=split("U,Y,M", ",")
| fields - _time
| eval min=min(mvfilter(NUMBERS>=0))
| eval Key=mvindex(KEY, mvfind(NUMBERS, min))

The last two lines finds the smallest number and then uses that minimum to find the offset into the KEY array.

Note that this does handle the case where min is not set because there is no value >= 0, so you'd have to put that in. It also assumes a 1:1 relationship with the size of NUMBERS and KEYS.

Hope this helps

 

View solution in original post

venkatasri
SplunkTrust
SplunkTrust

Hi @andres91302 

If you are on 8.x of Splunk version following works for your case, let me know how you go.

index=your_index sourcetype=your_st
| stats values(numbers) as n values(KEY) as KEY by client 
| eval min_list=mvmap(n, if(n < 0, n-n, n)) 
| eval min=min(mvmap(min_list, if(min_list >0, min_list, 999999)))

---

An upvote would be appreciated and accept solution if it helps!

bowesmana
SplunkTrust
SplunkTrust

Here's a solution that first looks for the minimum value (NUMBERS>=0) and then takes the Key from the KEY values

| makeresults
| eval Client="NATALIE", NUMBERS=split("-7,8,5",","), KEY=split("U,Y,M", ",")
| fields - _time
| eval min=min(mvfilter(NUMBERS>=0))
| eval Key=mvindex(KEY, mvfind(NUMBERS, min))

The last two lines finds the smallest number and then uses that minimum to find the offset into the KEY array.

Note that this does handle the case where min is not set because there is no value >= 0, so you'd have to put that in. It also assumes a 1:1 relationship with the size of NUMBERS and KEYS.

Hope this helps

 

Get Updates on the Splunk Community!

Splunk Forwarders and Forced Time Based Load Balancing

Splunk customers use universal forwarders to collect and send data to Splunk. A universal forwarder can send ...

NEW! Log Views in Splunk Observability Dashboards Gives Context From a Single Page

Today, Splunk Observability releases log views, a new feature for users to add their logs data from Splunk Log ...

Last Chance to Submit Your Paper For BSides Splunk - Deadline is August 12th!

Hello everyone! Don't wait to submit - The deadline is August 12th! We have truly missed the community so ...