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!

Index This | I am a number, but when you add ‘G’ to me, I go away. What number am I?

March 2024 Edition Hayyy Splunk Education Enthusiasts and the Eternally Curious!  We’re back with another ...

What’s New in Splunk App for PCI Compliance 5.3.1?

The Splunk App for PCI Compliance allows customers to extend the power of their existing Splunk solution with ...

Extending Observability Content to Splunk Cloud

Register to join us !   In this Extending Observability Content to Splunk Cloud Tech Talk, you'll see how to ...