Splunk Search

How to display one to one mapping between fields in stats command

neerajs_81
Builder

Hi there,  how can i use stats command to one to one mapping between fields .  I have tried "list" function and "values" function both but results are not expected.
Example: we are consolidating data from 2 indexes and both indexes have same fields of interests ( user, src_ip) 
Base query:

 

index=okta or index=network
| iplocation (src_ip)
|stats values(src_ip) values(deviceName) values(City) values(Country) by user, index

 



Results:
We get something like this

userindexsrc_ipDeviceNameCountry
John_smithokta10.0.0.1
192.178.2.24
laptop01USA
John_smithnetwork198.20.0.14
64.214.71.89
64.214.71.90
71.29.100.90
laptop01
laptop02
server01
My-CloudPC
USA
     


Expected results:
How to map which src_ip is coming from which Devicename?  We want to align the Devicename  in same sequence as per the src_ip ?

If i use list instead of values in my stats,  it shows duplicates like this for src_ip and deviceName. Even doing a |dedup src_ip is not helping 

neerajs_81_0-1741004055398.png

 



Hope clear.

Labels (1)
Tags (1)
0 Karma
1 Solution

richgalloway
SplunkTrust
SplunkTrust

The values and list functions display results in lexicographic order and destroy any potential relationship among the fields.  One solution is use mvzip to combine fields, group the results, then unzip the fields.

index=okta or index=network
| iplocation (src_ip)
| eval tuple = mvzip(src_ip, mvzip(deviceName, mvzip(City, Country)))
| stats values(tuple) by user, index
| eval fields = split(tuple, ",")
| eval src_ip = mvindex(fields, 0), deviceName=mvindex(fields,1), City=mvindex(fields, 2), Country=mvindex(fields,3)

A better approach might be to perform the iplocation command after stats.

index=okta or index=network
| stats values(src_ip) as src_ip by user, index
| mvexpand src_ip
| iplocation (src_ip)

 

---
If this reply helps you, Karma would be appreciated.

View solution in original post

richgalloway
SplunkTrust
SplunkTrust

The values and list functions display results in lexicographic order and destroy any potential relationship among the fields.  One solution is use mvzip to combine fields, group the results, then unzip the fields.

index=okta or index=network
| iplocation (src_ip)
| eval tuple = mvzip(src_ip, mvzip(deviceName, mvzip(City, Country)))
| stats values(tuple) by user, index
| eval fields = split(tuple, ",")
| eval src_ip = mvindex(fields, 0), deviceName=mvindex(fields,1), City=mvindex(fields, 2), Country=mvindex(fields,3)

A better approach might be to perform the iplocation command after stats.

index=okta or index=network
| stats values(src_ip) as src_ip by user, index
| mvexpand src_ip
| iplocation (src_ip)

 

---
If this reply helps you, Karma would be appreciated.
Career Survey
First 500 qualified respondents will receive a $20 gift card! Tell us about your professional Splunk journey.
Get Updates on the Splunk Community!

Observe and Secure All Apps with Splunk

 Join Us for Our Next Tech Talk: Observe and Secure All Apps with SplunkAs organizations continue to innovate ...

What's New in Splunk Observability - August 2025

What's New We are excited to announce the latest enhancements to Splunk Observability Cloud as well as what is ...

Introduction to Splunk AI

How are you using AI in Splunk? Whether you see AI as a threat or opportunity, AI is here to stay. Lucky for ...