# Extended\_stats with nested buckets

**URL:** <https://discuss.elastic.co/t/extended-stats-with-nested-buckets/239470>\
**Category:** Elasticsearch\
**Created:** [July 1, 2020, 11:25am UTC](https://discuss.elastic.co/t/extended-stats-with-nested-buckets/239470 "2020-07-01T11:25:50Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Camilo\_Salomon](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/camilo_salomon/32/47277_2.png) [@Camilo\_Salomon](https://discuss.elastic.co/u/Camilo_Salomon)\
**Post date:** [July 1, 2020, 11:25am UTC](https://discuss.elastic.co/t/extended-stats-with-nested-buckets/239470/1 "2020-07-01T11:25:50Z")

</div>

Hi everyone,

I'm new to elastic, so this question could be quite trivial for you, but here I go:

I'm using **kibana\_sample\_data\_ecommerce** , which documents have a mapping like this

```
{
	...
	"order_date" : <datetime>
	"taxful_total_price" : <double>
	...
}

```

And, I want to get a basic daily behavior of the data:

 ![q1](https://us1.discourse-cdn.com/elastic/original/3X/3/c/3c8cfafdd5d52283889ed73970ce569c7ea35618.png)

As far as I know, I have to split my data two times, first daily and then into the interval I wish (1h in this case). After that I have to sum the ocurrences on that time range (the little one) and to do a cumulative sum over the entire day for each day. To get something like this:

 ![q2](https://us1.discourse-cdn.com/elastic/original/3X/b/a/ba99e4ad63dc60646bda2ecc7c410c9b59100b0e.png)

So here is my query code:

```
POST kibana_sample_data_ecommerce/_search
{
  "size": 0,
  "query": {
    "bool": {
      "must": [
        {
          "range": {
            "order_date": {
              "gt": "now-1M",
              "lte": "now"
            }
          }
        }
      ]
    }
  },
  "aggs": {
    "day_histo": {
      "date_histogram": {
        "field": "order_date",
        "calendar_interval": "day"
      },
      "aggs": {
        "qmin_histo": {
          "date_histogram": {
            "field": "order_date",
            "calendar_interval": "hour"
          },
          "aggs": {
            "qminute_sum": {
              "sum": {
                "field": "taxful_total_price"
              }
            },
            "cumulative_qminute_sum": {
              "cumulative_sum": {
                "buckets_path": "qminute_sum"
              }
            }
          }
        }
      }
    }
  }
}

```

After doing that, I just have to get "extended stats" iterating the hours list for all days, _i.e._,

```auto
for hour in hours:
    extended_stats(day1[hour], day2[hour], ... , day30[hour]) 

```

But this last step is driving me crazy because this process give me nested buckets, for each time interval... So the thing is, how can I do an extended stats aggregation over the month data, but only on each time window across every day?

---

<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:** [July 29, 2020, 11:25am UTC](https://discuss.elastic.co/t/extended-stats-with-nested-buckets/239470/2 "2020-07-29T11:25:51Z")

</div>

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