Splunk Search

How to use addcoltotals to calculate percentage?

LearningGuy
Motivator


How to use addcoltotals to calculate percentage?
For example:  my search below   scoreSum % is empty 
Thank you for your help

index=test
| stats sum(score) as scoreSum by vuln
| addcoltotals labelfield =vuln   label=Total_scoreSum scoreSum
| eval scoreSum_pct = scoreSum/Total_scoreSum*100 . "%"
| table vuln, scoreSum, scoreSum_pct

Result:

vulnscoreSumscoreSum %
vulnA20 
vulnB40 
vulnC80 
Total_scoreSum140 


Expected result

vulnscoreSumscoreSum_pct
vulnA2014.3%
vulnB4028.6%
vulnC8057.1%
Total_scoreSum140100%
Labels (1)
0 Karma
1 Solution

ITWhisperer
SplunkTrust
SplunkTrust

Try something like this

index=test
| stats sum(score) as scoreSum by vuln
| eventstats sum(scoreSum) as total
| eval scoreSum_pct=100*scoreSum/total
| fields - total
| addcoltotals labelfield =vuln   label=Total_scoreSum scoreSum scoreSum_pct
| eval scoreSum_pct = round(scoreSum_pct,1) . "%"
| table vuln, scoreSum, scoreSum_pct

View solution in original post

ITWhisperer
SplunkTrust
SplunkTrust

Try something like this

index=test
| stats sum(score) as scoreSum by vuln
| eventstats sum(scoreSum) as total
| eval scoreSum_pct=100*scoreSum/total
| fields - total
| addcoltotals labelfield =vuln   label=Total_scoreSum scoreSum scoreSum_pct
| eval scoreSum_pct = round(scoreSum_pct,1) . "%"
| table vuln, scoreSum, scoreSum_pct

LearningGuy
Motivator

Hello,
I tried your suggestion and it worked successfully. I accepted this as solution. I appreciate your help. Thank you.

1)    If I printed out "total" field, it can give total 140 in each row.   
How did eventstats know how to calculate the total of 140, when "stats" command has scoreSum of 20/40/80?         
If I played around and used "stats" or "eventstats group by vuln", it didn't work.  Please suggest

         | eventstats sum(scoreSum) as total    

vulnscoreSumtotalscoreSum_pct
vulnA2014014.3%
vulnB4014028.6%
vulnC8014057.1%
Total_scoreSum140420100%



2)   If there are hundreds of row, there will be multi pages. The "Total_scoreSum" field will appear at the end of the row.
       Is there a way to display it on the first page, but at the bottom, not at the top (using sort)?
| addcoltotals labelfield =vuln label=Total_scoreSum scoreSum scoreSum_pct

Thank you so much

0 Karma

ITWhisperer
SplunkTrust
SplunkTrust

1) eventstats adds the aggregated value (sum in this instance) to each event, stats replaces the events with the aggregated statisitcs

2) No, this is not normally possible - addtotals adds an extra event (row) to the pipeline at the end. The way the pipeline is displayed happens after the total row has been added and there is no way to predict how big the first page of the display is going to be ahead of time.

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!

Can’t Make It to Boston? Stream .conf25 and Learn with Haya Husain

Boston may be buzzing this September with Splunk University and .conf25, but you don’t have to pack a bag to ...

Splunk Lantern’s Guide to The Most Popular .conf25 Sessions

Splunk Lantern is a Splunk customer success center that provides advice from Splunk experts on valuable data ...

Unlock What’s Next: The Splunk Cloud Platform at .conf25

In just a few days, Boston will be buzzing as the Splunk team and thousands of community members come together ...