Splunk Search

How to deduct two sums

highriser666
New Member

I am trying to sum 2 Fields of a search and then deduct the one from the other:

my idea is not working:

| stats sum( eval (stats sum(Field1)) - (stats( sum(Field2)) ) AS difference12

0 Karma

fdi01
Motivator

or you can try like :

...| timechart eval(sum(Field1)- sum(Field2)) as difference12

0 Karma

MuS
SplunkTrust
SplunkTrust

this will not work, because sum() is not an eval function, but the other way around it will work (run everywhere example):

 index=_internal | timechart sum(eval(kb-kbps)) AS diff

But I doubt this will be the correct result, because it will not calculate the total difference. On the other hand the use case is not 100% clear so maybe it could work 😉

0 Karma

s2_splunk
Splunk Employee
Splunk Employee

How about:

| stats sum(Field1) as Sum1, sum(Field2) as Sum2 | eval difference12 = Sum1-Sum2
0 Karma

MuS
SplunkTrust
SplunkTrust

Hi highriser666,

take this run everywhere command as example:

 index=_internal | stats sum(kb) AS KB sum(kbps) AS KBPS | eval diff=KB-KBPS

You frist have to sum() your fields (here it is kb and kbps) and then evaluate the difference.

Hope this helps ...

cheers, MuS

0 Karma

highriser666
New Member

Thank you,

I tried is already before getting mad with the other term.

When I do as you suggest the stats it gives me always only the sums for field1 and field2.

"Difference12" is not shown as the expected single value in the stats-tab of the results even though it should be the
singular result of the term.

Stats always only shows field1 and field2.

Currently Field2 is all time NULL could that cause an issue?

0 Karma

MuS
SplunkTrust
SplunkTrust

either do as @somesoni2 suggests or provide some sample events and your complete search. Makes it easier to help 😉

0 Karma

somesoni2
Revered Legend

Yes, if the value of a field is always NULL, the stats will return NULL for the Field2 and subsequent diff will also be NULL.

Try something like this

Your base search  | stats sum(Field1) AS Field1 sum(Field2) AS Field2  | fillnull value=0 Field2 | eval Difference12 =Field1 -Field1 
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!

Where Innovation Takes Flight: The Splunk4Aviation Flight Sim Lands at .conf26

If you hear someone at .conf26 shouting "gear down, GEAR DOWN" across the show floor, you have found us.  The ...

Turn Cisco Telemetry Into Action with Cisco Data Fabric, powered by the Splunk ...

The surge in machine data is already hitting enterprise budgets, and the agentic era will only intensify it. ...

Persistent Queue at TcpOut — One of Splunk's Most Practical Features

Splunk introduced persistent queueing at the tcpout layer as one of the most practical resilience features in ...