Splunk Search

How to get the count of unique values for a field per event?

Aaron_Fogarty
Path Finder

I have a field named Visit that can have several vales: "Order Entry", "Order Reference" and 5 others. I want to count every Visit per event only once per value.

What I mean by this is that in Example one, the Visit count for this event will be only 1
and in Example two, the Visit count for the event will be 2.

Event Example one:

Client - User:123 - Visit="Order Entry" - Apr 25, 2016, 9:34:58 AM EDT
Client - User:123 - New selection begin  - Apr 25, 2016, 9:34:58 AM EDT
Client - User:123 - New selection begin  - Apr 25, 2016, 9:34:58 AM EDT
Client - User:123 - Loading selection begin  - Apr 25, 2016, 9:34:58 AM EDT
Client - User:123 - Visit="Order Entry" - Apr 25, 2016, 9:34:58 AM EDT

Event Example two:

Client - User:456 - Visit="Order Entry" - Apr 25, 2016, 6:46:22 AM EDT
Client - User:456 - New order begin  - Apr 25, 2016, 6:46:22 AM EDT
Client - User:456 - Visit="Order Reference" - Apr 25, 2016, 6:46:22 AM EDT
Client - User:456 - Successful login.  Failed login attempts 0.

Thank you.

0 Karma
1 Solution

javiergn
Super Champion

Hi,

If your events are separated as you described above, you can try this:

your search here
| rex max_match=0 "Visit=(?<visits>\"[^\"]+\")"
| mvexpand visits
| stats values(visits) as visits dc(visits) as visit_counts by _raw

Example 1:

| stats count
| eval _raw = "
 Client - User:123 - Visit=\"Order Entry\" - Apr 25, 2016, 9:34:58 AM EDT
 Client - User:123 - New selection begin  - Apr 25, 2016, 9:34:58 AM EDT
 Client - User:123 - New selection begin  - Apr 25, 2016, 9:34:58 AM EDT
 Client - User:123 - Loading selection begin  - Apr 25, 2016, 9:34:58 AM EDT
 Client - User:123 - Visit=\"Order Entry\" - Apr 25, 2016, 9:34:58 AM EDT
"
| rex max_match=0 "Visit=(?<visits>\"[^\"]+\")"
| mvexpand visits
| stats values(visits) as visits dc(visits) as visit_counts by _raw

# visit_counts = 1 

Example 2:

| stats count
| eval _raw = "
 Client - User:456 - Visit=\"Order Entry\" - Apr 25, 2016, 6:46:22 AM EDT
 Client - User:456 - New order begin  - Apr 25, 2016, 6:46:22 AM EDT
 Client - User:456 - Visit=\"Order Reference\" - Apr 25, 2016, 6:46:22 AM EDT
 Client - User:456 - Successful login.  Failed login attempts 0.
"
| rex max_match=0 "Visit=(?<visits>\"[^\"]+\")"
| mvexpand visits
| stats values(visits) as visits dc(visits) as visit_counts by _raw


# visit_counts = 2

View solution in original post

0 Karma

javiergn
Super Champion

Hi,

If your events are separated as you described above, you can try this:

your search here
| rex max_match=0 "Visit=(?<visits>\"[^\"]+\")"
| mvexpand visits
| stats values(visits) as visits dc(visits) as visit_counts by _raw

Example 1:

| stats count
| eval _raw = "
 Client - User:123 - Visit=\"Order Entry\" - Apr 25, 2016, 9:34:58 AM EDT
 Client - User:123 - New selection begin  - Apr 25, 2016, 9:34:58 AM EDT
 Client - User:123 - New selection begin  - Apr 25, 2016, 9:34:58 AM EDT
 Client - User:123 - Loading selection begin  - Apr 25, 2016, 9:34:58 AM EDT
 Client - User:123 - Visit=\"Order Entry\" - Apr 25, 2016, 9:34:58 AM EDT
"
| rex max_match=0 "Visit=(?<visits>\"[^\"]+\")"
| mvexpand visits
| stats values(visits) as visits dc(visits) as visit_counts by _raw

# visit_counts = 1 

Example 2:

| stats count
| eval _raw = "
 Client - User:456 - Visit=\"Order Entry\" - Apr 25, 2016, 6:46:22 AM EDT
 Client - User:456 - New order begin  - Apr 25, 2016, 6:46:22 AM EDT
 Client - User:456 - Visit=\"Order Reference\" - Apr 25, 2016, 6:46:22 AM EDT
 Client - User:456 - Successful login.  Failed login attempts 0.
"
| rex max_match=0 "Visit=(?<visits>\"[^\"]+\")"
| mvexpand visits
| stats values(visits) as visits dc(visits) as visit_counts by _raw


# visit_counts = 2
0 Karma

Aaron_Fogarty
Path Finder

That is perfect javiergn. Exactly what I was looking to do. Cheers.

0 Karma
Get Updates on the Splunk Community!

Developer Spotlight with Paul Stout

Welcome to our very first developer spotlight release series where we'll feature some awesome Splunk ...

State of Splunk Careers 2024: Maximizing Career Outcomes and the Continued Value of ...

For the past four years, Splunk has partnered with Enterprise Strategy Group to conduct a survey that gauges ...

Data-Driven Success: Splunk & Financial Services

Splunk streamlines the process of extracting insights from large volumes of data. In this fast-paced world, ...