Splunk Search

How do you get a distinct count of one field based on the value of another?

weidertc
Communicator

I need to count the total based on status, but also the number of sessions for each status. The number of sessions is the distinct count of the sessionId field.

| makeresults 1 | eval status="FAILURE", sessionId=2345 
| append [| makeresults 1 | eval status="FAILURE", sessionId=1234] 
| append [| makeresults 1 | eval status="FAILURE", sessionId=1234] 
| append [| makeresults 1 | eval status="FAILURE", sessionId=1234] 
| append [| makeresults 1 | eval status="SUCCESS", sessionId=1234] 
| stats count AS Total, dc(case(match(status, "FAILURE"), sessionId)) as "TotalSessions" by status

this gives me

Status, Total, TotalSessions
FAILURE, 4, 0
SUCCESS, 1, 0

but it needs to be

Status, Total, TotalSessions
FAILURE, 4, 2
SUCCESS, 1, 1

What am i doing incorrectly?

-Chris

0 Karma
1 Solution

chrisyounger
SplunkTrust
SplunkTrust

Hi Chris,

Try this

 | makeresults 1 | eval status="FAILURE", sessionId=2345 
 | append [| makeresults 1 | eval status="FAILURE", sessionId=1234] 
 | append [| makeresults 1 | eval status="FAILURE", sessionId=1234] 
 | append [| makeresults 1 | eval status="FAILURE", sessionId=1234] 
 | append [| makeresults 1 | eval status="SUCCESS", sessionId=1234]
 | stats count AS Total, dc(sessionId) as "TotalSessions" by status

All the best

Edit: sorry updated

View solution in original post

0 Karma

chrisyounger
SplunkTrust
SplunkTrust

Hi Chris,

Try this

 | makeresults 1 | eval status="FAILURE", sessionId=2345 
 | append [| makeresults 1 | eval status="FAILURE", sessionId=1234] 
 | append [| makeresults 1 | eval status="FAILURE", sessionId=1234] 
 | append [| makeresults 1 | eval status="FAILURE", sessionId=1234] 
 | append [| makeresults 1 | eval status="SUCCESS", sessionId=1234]
 | stats count AS Total, dc(sessionId) as "TotalSessions" by status

All the best

Edit: sorry updated

0 Karma

weidertc
Communicator

ah, I was over-thinking it. Long day. It works. thanks!

0 Karma
Get Updates on the Splunk Community!

Stay Connected: Your Guide to May Tech Talks, Office Hours, and Webinars!

Take a look below to explore our upcoming Community Office Hours, Tech Talks, and Webinars this month. This ...

They're back! Join the SplunkTrust and MVP at .conf24

With our highly anticipated annual conference, .conf, comes the fez-wearers you can trust! The SplunkTrust, as ...

Enterprise Security Content Update (ESCU) | New Releases

Last month, the Splunk Threat Research Team had two releases of new security content via the Enterprise ...