Splunk Search

Counting occurences in aggregation by fields

RonaldCWWong
Explorer

Hi community,

I have a question on counting the number of events per values() value in stats command.
For example having events with src_ip, user (and a couple of more) fields.
I would like to count each of the user occurence in the raw log. Example as below.

 | stats values(user) as values_user by src_ip 


Example:

_timeusersrc_ip
2025-08-11

ronald

192.168.2.5
2025-08-11jasmine 192.168.2.5
2025-08-11tim192.168.2.6
2025-08-11

ronald

192.168.2.5

 

I would like to have result as 

values_usercount_vaules_usersrc_ip
ronald
jasmine

ronald:2

jasmine:1

192.168.2.5
timtim:1192.168.2.6



Labels (5)
0 Karma
1 Solution

PrewinThomas
Motivator

@RonaldCWWong 

If I understood you correctly, you want to group by src_ip, list all the distinct user values per IP, and also count how many times each user appears for that IP.

try below,

...your base search...
| stats count by src_ip, user
| eventstats sum(count) as user_count by src_ip, user
| eval user_count_pair = user . ":" . user_count
| stats values(user) as values_user values(user_count_pair) as count_values_user by src_ip

demo1.JPG

 

 

Regards,
Prewin
Splunk Enthusiast | Always happy to help! If this answer helped you, please consider marking it as the solution or giving a Karma. Thanks!

View solution in original post

bowesmana
SplunkTrust
SplunkTrust

Just use a couple of stats, first count the user numbers then create a new field with the user and count then re-stats with the values, e.g.

| makeresults format=csv data="_time,user,src_ip
2025-08-11,ronald,192.168.2.5
2025-08-11,jasmine,192.168.2.5
2025-08-11,tim,192.168.2.6
2025-08-11,ronald,192.168.2.5"
``` Like this ```
| stats count by user src_ip
| eval user_count=user.":".count
| stats values(user*) as values_user* by src_ip

RonaldCWWong
Explorer

thanks for help 🙂

0 Karma

PrewinThomas
Motivator

@RonaldCWWong 

If I understood you correctly, you want to group by src_ip, list all the distinct user values per IP, and also count how many times each user appears for that IP.

try below,

...your base search...
| stats count by src_ip, user
| eventstats sum(count) as user_count by src_ip, user
| eval user_count_pair = user . ":" . user_count
| stats values(user) as values_user values(user_count_pair) as count_values_user by src_ip

demo1.JPG

 

 

Regards,
Prewin
Splunk Enthusiast | Always happy to help! If this answer helped you, please consider marking it as the solution or giving a Karma. Thanks!

RonaldCWWong
Explorer

thanks mate, this is exactly what I am looking for 🙂

0 Karma
Career Survey
First 500 qualified respondents will receive a $20 gift card! Tell us about your professional Splunk journey.

Can’t make it to .conf25? Join us online!

Get Updates on the Splunk Community!

Can’t Make It to Boston? Stream .conf25 and Learn with Haya Husain

Boston may be buzzing this September with Splunk University and .conf25, but you don’t have to pack a bag to ...

Splunk Lantern’s Guide to The Most Popular .conf25 Sessions

Splunk Lantern is a Splunk customer success center that provides advice from Splunk experts on valuable data ...

Unlock What’s Next: The Splunk Cloud Platform at .conf25

In just a few days, Boston will be buzzing as the Splunk team and thousands of community members come together ...