Splunk Search

How to find the sum of FieldA for each distinct FieldB and timechart the average of each unique sum?

mibaker_arrow_c
New Member

Invc Sales Order Number = Invoice # that will exist across multiple events
Inv = $$

I need to sum the Inv for Each Distinct "Invc Sales Order Number", then average the total to arrive at an Average for each unique sum. Then i would like to timechart it to show trend.

With the search below I am able to calculate and display an accurate Average, but am not able to timechart the Average below:

......| stats dc("Invc Sales Order Num") as Invoices sum(Inv) as Revenue | eval Average=Revenue/Invoices

Tags (3)
0 Karma

woodcock
Esteemed Legend

Try this:

... | stats latest(_time) AS _time, sum(Inv) AS InvoiceTotal BY "Invc Sales Order Number" | timechart avg(InvoiceTotal)
0 Karma

jacobwilkins
Communicator

Does each distinct invoice number only have one value of _time associated with it? Lets assume that you are OK with using the value of _time from the last event for a particular invoice. Lets also use rename on that nasty field name.

| rename "Invc Sales Order Num" AS InvoiceNum
| stats latest(_time) AS _time, sum(Inv) AS totalInv by InvoiceNum
| timechart avg(totalInv)
0 Karma

aweitzman
Motivator

Your stats line doesn't appear to give you what you described; it appears to give you an overall average rather than an average per invoice number. It seems like you would want something like this instead:

| timechart avg(Inv) as AvgRevenue by "Invc Sales Order Num"

Or am I misunderstanding this?

0 Karma
Get Updates on the Splunk Community!

[Puzzles] Solve, Learn, Repeat: Dynamic formatting from XML events

This challenge was first posted on Slack #puzzles channelFor a previous puzzle, I needed a set of fixed-length ...

Enter the Agentic Era with Splunk AI Assistant for SPL 1.4

  🚀 Your data just got a serious AI upgrade — are you ready? Say hello to the Agentic Era with the ...

Stronger Security with Federated Search for S3, GCP SQL & Australian Threat ...

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