Splunk Search

How to use stats to join 2 indexes based on 1 field

leonheart78
Explorer

Currently I have 2 indexes:
Index A contains ProgramID, User
Index B contains ID, Machine

I would like to use stats to join these 2 indexes based on ProgramID and ID, and get a table to reflect the User, Machine, and the Percent count of the Machine in the record.

How can I do it?
Thank you.

0 Karma
1 Solution

Runals
Motivator

I'm not quite sure what you mean by percent count of the machine. To join the data and assuming ID = ProgramID I'd probably do something like

index=A OR index=B sourcetype=... OR sourcetype=... | foreach User Machine [eval <<FIELD>> = coalesce(<<FIELD>>, "-")] | eval ProgramID = coalesce(ProgramID, ID) | stats count by Machine, User, ProgramID

I'm first making sure the fields User and Machine have data since they will be used in the stats command. Then I'm collapsing ProgramID and ID (based on my assumptions). Lastly a simple stats command to get the number of events per each Machine, User & ProgramID combo. If you want percent of something related to machine (ie how often does this machine or program show up relative to the result set) you probably would then need to use an eventstats command.

View solution in original post

mtranchita
Communicator

Would something like this get you what you are looking for?
index="Index A" | fields ProgramID, User | rename ProgramID AS ID | search [ index="Index B" | fields ID, Machine] | stats c(ID) as ID by User, Machine

0 Karma

Runals
Motivator

I'm not quite sure what you mean by percent count of the machine. To join the data and assuming ID = ProgramID I'd probably do something like

index=A OR index=B sourcetype=... OR sourcetype=... | foreach User Machine [eval <<FIELD>> = coalesce(<<FIELD>>, "-")] | eval ProgramID = coalesce(ProgramID, ID) | stats count by Machine, User, ProgramID

I'm first making sure the fields User and Machine have data since they will be used in the stats command. Then I'm collapsing ProgramID and ID (based on my assumptions). Lastly a simple stats command to get the number of events per each Machine, User & ProgramID combo. If you want percent of something related to machine (ie how often does this machine or program show up relative to the result set) you probably would then need to use an eventstats command.

MuS
SplunkTrust
SplunkTrust

Nice answer! Just to add a link to another answer which could be helpful in this case https://answers.splunk.com/answers/129424/how-to-compare-fields-over-multiple-sourcetypes-without-jo...

cheers, MuS

0 Karma

wrangler2x
Motivator

And here is a Splunk blog entry on coalesce: http://blogs.splunk.com/2014/03/21/search-command-coalesce/

0 Karma
Get Updates on the Splunk Community!

Splunk Observability for AI

Don’t miss out on an exciting Tech Talk on Splunk Observability for AI!Discover how Splunk’s agentic AI ...

Splunk Enterprise Security 8.x: The Essential Upgrade for Threat Detection, ...

Watch On Demand the Tech Talk on November 6 at 11AM PT, and empower your SOC to reach new heights! Duration: ...

Splunk Observability as Code: From Zero to Dashboard

For the details on what Self-Service Observability and Observability as Code is, we have some awesome content ...