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!

Fueling your curiosity with new Splunk ILT and eLearning courses

At Splunk Education, we’re driven by curiosity—both ours and yours! That’s why we’re committed to delivering ...

Splunk AI Assistant for SPL 1.1.0 | Now Personalized to Your Environment for Greater ...

Splunk AI Assistant for SPL has transformed how users interact with Splunk, making it easier than ever to ...

Unleash Unified Security and Observability with Splunk Cloud Platform

     Now Available on Microsoft AzureOn Demand Now Step boldly into the AI revolution with enhanced security ...