cliqer
cliqer

Reputation: 271

CloudWatch Logs Insights get average of count returned

I'm querying CloudWatch logs via Logs Insights queries and am trying to get the average for count returned. For example, my current query that gets the counts is (only relevant parts shown):

fields fieldA as A
| stats count(*) as countForEachA by A

returns this:

  A    countForEachA
______ _____________
a123      22
a124      22
a125      16

I'm trying to get the average for field countForEachA. Therefore, for the example above, I want the average: 20 (sum of countForEachA divided by total results). Here's what I've tried:

fields fieldA as A
| stats avg(count(*)) as average by A

The query above returns this:

  A    countForEachA
______ _____________
a123      2.3
a124      1.4
a125      2.9

while I expected a single answer representing the average. Any help will be appreciated.

Upvotes: 5

Views: 7934

Answers (1)

cliqer
cliqer

Reputation: 271

I got it, to get the average for countForEachA, we first need to get the count value and then simply call the avg() function like so:

fields fieldA as A
| stats count(*) as countForEachA, avg(countForEachA)

Hope this helps others.

Upvotes: 6

Related Questions