Splunk Search

counting results in sub-query

stephenmoorhous
Path Finder

I have a list of log lines which indicate an order has been placed and have a session id (sid) but no customer id (cid)
I also have a list of log lines which have a session id and a customer id

so
index=foo sourcetype=bar sid=123 orderplaced=1
index=foo sourcetype=zzz sid=123 cid=1 this=some
index=foo sourcetype=zzz sid=123 cid=1 this=random
index=foo sourcetype=zzz sid=123 cid=1 this=stuff
index=foo sourcetype=bar sid=123 orderplaced=1
index=foo sourcetype=zzz sid=123 cid=1 this=more
index=foo sourcetype=zzz sid=123 cid=1 this=random
index=foo sourcetype=zzz sid=123 cid=1 this=thing
index=foo sourcetype=bar sid=321 orderplaced=1
index=foo sourcetype=zzz sid=321 cid=2 this=etc
index=foo sourcetype=zzz sid=555 cid=3 this=etc

etc

what i want is a count of how many orders each customer has placecd so the above would produce
cid count
1 2
2 1
3 0

I have
index=foo sourcetype=zzz [search index=foo sourcetype=bar orderplaced=1 | fields sid] | stats count by cid
but this counts all the log lines in foo so cid=1 gets a count of 6

I'm sure there is a simple solution but can think of it

Thanks

Tags (1)
0 Karma
1 Solution

cmerriman
Super Champion

try this: index=foo sourcetype=zzz |join sid [search index=foo sourcetype=bar orderplaced=1 | fields sid orderplaced] |fillnull orderplaced value=0|stats values(cid) as cid sum(orderplaced) as orderplaced by sid|stats sum(orderplaced) as orders by cid

View solution in original post

cmerriman
Super Champion

try this: index=foo sourcetype=zzz |join sid [search index=foo sourcetype=bar orderplaced=1 | fields sid orderplaced] |fillnull orderplaced value=0|stats values(cid) as cid sum(orderplaced) as orderplaced by sid|stats sum(orderplaced) as orders by cid

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!

Community Content Calendar, September edition

Welcome to another insightful post from our Community Content Calendar! We're thrilled to continue bringing ...

Splunkbase Unveils New App Listing Management Public Preview

Splunkbase Unveils New App Listing Management Public PreviewWe're thrilled to announce the public preview of ...

Leveraging Automated Threat Analysis Across the Splunk Ecosystem

Are you leveraging automation to its fullest potential in your threat detection strategy?Our upcoming Security ...