Splunk Search

Calculate percentage from a join query

justjosh
Explorer

I need to calculate the percentage of products that I have searched for that exist in a specific product catalog.

I have written an inner join, which gets all the product_ids from the search and does as inner join into the product catalog:

sourcetype=products | fields product_id |join product_id [search sourcetype=catalogue |fields product_id ] | stats count

I added stats count which gives the number of products from the search in the catalog.

How can I modify the query so it returns the count as a percentage of total products that were returned from the initial search?

Tags (2)
0 Karma

justgrumpy
Path Finder

I'm not sure if there is some built-in function or command to get what you want, but I believe the following query will get you close:

sourcetype=products
 | stats dc(product_id) as count by product_id
 | eventstats dc(product_id) as totalCount
 | join product_id [search sourcetype=catalogue]
 | stats count as lookupCount values(totalCount) as totalCount
 | eval percTotal=lookupCount/totalCount
  1. The first stats line gets you a distinct list of product ids that will be needed later for the join.
  2. The eventstats line adds a count of the number of products found to each result from the previous stats. This is needed later for the calculation of percentage of total products.
  3. The join is basically the same except I removed the fields.
  4. The next stats line gets a count of the items found from the join as well as carrying over the totalCount from the previous results.
  5. The eval gets your percentage of total products that were returned from the initial search.
0 Karma
Got questions? Get answers!

Join the Splunk Community Slack to learn, troubleshoot, and make connections with fellow Splunk practitioners in real time!

Meet up IRL or virtually!

Join Splunk User Groups to connect and learn in-person by region or remotely by topic or industry.

Get Updates on the Splunk Community!

SOC4Kafka - New Kafka Connector Powered by OpenTelemetry

The new SOC4Kafka connector, built on OpenTelemetry, enables the collection of Kafka messages and forwards ...

Event Series: Level up your SOC: Advancing with Splunk Enterprise Security

AI has fundamentally raised the stakes for security operations, and this three-part series is your guide to ...

Announcing Modern Navigation: A New Era of Splunk User Experience

We are excited to introduce the Modern Navigation feature in the Splunk Platform, available to both cloud and ...