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!

Upcoming Webinar: Unmasking Insider Threats with Slunk Enterprise Security’s UEBA

Join us on Wed, Dec 10. at 10AM PST / 1PM EST for a live webinar and demo with Splunk experts! Discover how ...

.conf25 technical session recap of Observability for Gen AI: Monitoring LLM ...

If you’re unfamiliar, .conf is Splunk’s premier event where the Splunk community, customers, partners, and ...

A Season of Skills: New Splunk Courses to Light Up Your Learning Journey

There’s something special about this time of year—maybe it’s the glow of the holidays, maybe it’s the ...