# Average per day for a period

**URL:** <https://discuss.elastic.co/t/average-per-day-for-a-period/308942>\
**Category:** Elasticsearch\
**Created:** [July 5, 2022, 4:31pm UTC](https://discuss.elastic.co/t/average-per-day-for-a-period/308942 "2022-07-05T16:31:38Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Igor-lkm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/igor-lkm/32/107937_2.png) [@Igor-lkm](https://discuss.elastic.co/u/Igor-lkm)\
**Post date:** [July 5, 2022, 4:31pm UTC](https://discuss.elastic.co/t/average-per-day-for-a-period/308942/1 "2022-07-05T16:31:38Z")

</div>

Hello,

I have a quite simple aggregation to get sum and average total price for a period (1 day, 7 days, 30 days etc.) from my index. That works quite good.

Aggregation for 30 days looks like this:

```auto
{
  "size": 10000,
  "aggs": {
    "30d": {
      "filter": {
          "range": {
            "data.timestamp": {
              "gt": "now-30d"
            }
        }
      },
      "aggs": {

        "avgTotalPrice": { "avg": { "field": "data.payload.totalPrice" } },
        "sumTotalPrice": { "sum": { "field": "data.payload.totalPrice" } }
      }
    },
}

```

Now I would like to get daily average, just one number.

In fact what I am looking for is `sumTotalPrice / amount of days`

What is the most elegant way to do this?

My first thought was to get (somehow) amount of days based on range filter and use it in scripted field. Like ` "gt": "now-30d"` is 30 days and ` "gt": "now-1m"` is amount of days depends of the month. I would like to ask if this is possible.. (as I failed to do this) And then do my calculations `sumTotalPrice / amount_of_days`.

Is it possible or is there a better, simple and elegant 😉 way?

Thank you!

---

<div class="post-metadata">

**Author:** ![Igor-lkm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/igor-lkm/32/107937_2.png) [@Igor-lkm](https://discuss.elastic.co/u/Igor-lkm)\
**Post date:** [July 20, 2022, 12:53pm UTC](https://discuss.elastic.co/t/average-per-day-for-a-period/308942/2 "2022-07-20T12:53:20Z")

</div>

At the end of the day I got what i need by using `date_histogram ` and `avg_bucket`:

Query:

```auto
{
  "size": 0,
  "aggs": {
    "3d": {
      "filter": {
          "range": {
            "data.timestamp": {
              "gt": "now-3d"
            }
        }
      },
      "aggs": {
        "order_per_period": {
           "date_histogram": {
              "field": "data.timestamp",
              "calendar_interval": "day"
            },
            "aggs": {
              "sum_total_price": {
               "sum": {
                  "field": "data.totalPrice"
                }
              }
            }
         },
         "avgSumTotalPrice": {
          "avg_bucket": {
            "buckets_path": "order_per_period>sum_total_price"
         }
       }
      }
    }
  }
}

```

Response:

```auto
{
  "took": 3,
  "timed_out": false,
  "_shards": {
    "total": 1,
    "successful": 1,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": {
      "value": 10000,
      "relation": "gte"
    },
    "max_score": null,
    "hits": []
  },
  "aggregations": {
    "30d": {
      "doc_count": 84,
      "order_per_period": {
        "buckets": [
          {
            "key_as_string": "2022-07-18T00:00:00.000Z",
            "key": 1658102400000,
            "doc_count": 26,
            "sum_total_price": {
              "value": 180
            }
          },
          {
            "key_as_string": "2022-07-19T00:00:00.000Z",
            "key": 1658188800000,
            "doc_count": 44,
            "sum_total_price": {
              "value": 390
            }
          },
          {
            "key_as_string": "2022-07-20T00:00:00.000Z",
            "key": 1658275200000,
            "doc_count": 13,
            "sum_total_price": {
              "value": 100
            }
          }
        ]
      },
      "avgSumTotalPrice": {
        "value": 233.33
      }
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [August 17, 2022, 12:53pm UTC](https://discuss.elastic.co/t/average-per-day-for-a-period/308942/3 "2022-08-17T12:53:46Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
