Splunk Search

Return one value from field-B when counting field-A

sta_splunk
Engager

I have data similar to:

Field-A Field-B
A1           B1
A1           B2
A1           B3
A2           B4
A3           B5
A2           B6

Where Field-A will repeat but Field-B is unique values. 

I am using | stats count by Field-A to give me the number of occurrences of A1, A2, A3 and am trying to include a single example of Field-B.  Something like:

Field-Count-Example
A1 -- 3 -- B2
A2 -- 2 -- B6
A3 -- 1 -- B5

Thank you for any suggestions.

 

Labels (2)
0 Karma
1 Solution

richgalloway
SplunkTrust
SplunkTrust

Try this

| stats count as Count, first(Field-B) as Example by Field-A
---
If this reply helps you, Karma would be appreciated.

View solution in original post

sta_splunk
Engager

That worked great! I was trying to use two different 'stats' and could not get both of the values.  

Thanks for your help!!

richgalloway
SplunkTrust
SplunkTrust

Two separate stats commands are unlikely to work because they're transforming commands.  That means the seconds stats won't have the same fields to work with as the first one.  One alternative is to use eventstats before stats, but it's unnecessary in this case because a single stats can do it all.

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

richgalloway
SplunkTrust
SplunkTrust

Try this

| stats count as Count, first(Field-B) as Example by Field-A
---
If this reply helps you, Karma would be appreciated.
Get Updates on the Splunk Community!

Splunk AI Assistant for SPL | Key Use Cases to Unlock the Power of SPL

Splunk AI Assistant for SPL | Key Use Cases to Unlock the Power of SPL  The Splunk AI Assistant for SPL ...

Buttercup Games: Further Dashboarding Techniques (Part 5)

This series of blogs assumes you have already completed the Splunk Enterprise Search Tutorial as it uses the ...

Customers Increasingly Choose Splunk for Observability

For the second year in a row, Splunk was recognized as a Leader in the 2024 Gartner® Magic Quadrant™ for ...