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
Legend

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!

Join Us for Splunk University and Get Your Bootcamp Game On!

If you know, you know! Splunk University is the vibe this summer so register today for bootcamps galore ...

.conf24 | Learning Tracks for Security, Observability, Platform, and Developers!

.conf24 is taking place at The Venetian in Las Vegas from June 11 - 14. Continue reading to learn about the ...

Announcing Scheduled Export GA for Dashboard Studio

We're excited to announce the general availability of Scheduled Export for Dashboard Studio. Starting in ...