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!
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
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!
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