Splunk Search

calculating percentage using eval and stats commands

manju1318
Engager

Hi, I am working on a task: calculating the percentage of employees working in food industry for each country. I tried to develop the code but its not working. please correct my where its mistake.


| stats sum("Number of employees") as Total_Emp by Country
| where Industry="Food*"
| stats sum("Number of employees") as Food_Emp by Country
| eval Percent = round((Food_Emp/Total_Emp)*100,2)."%"

Labels (3)
0 Karma
1 Solution

PickleRick
SplunkTrust
SplunkTrust

Each command in the search pipeline knows only the results from preceeding commands (probably that's why the pipe sign to separate them because it's very similar to shell stream processing). So as soon as you do the first stats command you lose the information about raw data and can only operate on aggregated results.

There are several possible approaches to your problem but I'd do it like this:

| stats sum('Number of employees') as overall sum(eval(if(like(Industry,"Food%"),'Number of employees',0))) as food by Country

(Might have some mismatched parentheses)

 

View solution in original post

manju1318
Engager

thank you for responding.

0 Karma

PickleRick
SplunkTrust
SplunkTrust

Each command in the search pipeline knows only the results from preceeding commands (probably that's why the pipe sign to separate them because it's very similar to shell stream processing). So as soon as you do the first stats command you lose the information about raw data and can only operate on aggregated results.

There are several possible approaches to your problem but I'd do it like this:

| stats sum('Number of employees') as overall sum(eval(if(like(Industry,"Food%"),'Number of employees',0))) as food by Country

(Might have some mismatched parentheses)

 

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!

Why Splunk Customers Should Attend Cisco Live 2026 Las Vegas

Why Splunk Customers Should Attend Cisco Live 2026 Las Vegas     Cisco Live 2026 is almost here, and this ...

What Is the Name of the USB Key Inserted by Bob Smith? (BOTS Hint, Not the Answer)

Hello Splunkers,   So you searched, “what is the name of the usb key inserted by bob smith?”  Not gonna lie… ...

Automating Threat Operations and Threat Hunting with Recorded Future

    Automating Threat Operations and Threat Hunting with Recorded Future June 29, 2026 | Register   Is your ...