# Stats\_bucket the correct query here?

**URL:** <https://discuss.elastic.co/t/stats-bucket-the-correct-query-here/169931>\
**Category:** Elasticsearch\
**Created:** [February 26, 2019, 2:26am UTC](https://discuss.elastic.co/t/stats-bucket-the-correct-query-here/169931 "2019-02-26T02:26:50Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![jjasonclark](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jjasonclark/32/41313_2.png) [@jjasonclark](https://discuss.elastic.co/u/jjasonclark)\
**Post date:** [February 26, 2019, 2:26am UTC](https://discuss.elastic.co/t/stats-bucket-the-correct-query-here/169931/1 "2019-02-26T02:26:50Z")

</div>

Question:

- How do I get the average sales the clerk's are doing in each region and store?
- How do I not include `clerkSums` in the responses? I only care about `salesStats` and `costStats`?

* * *

Imagine I have a single index with 2 number columns, `sales` and `cost`. And 3 string columns, `region`, `store`, `clerk`.

How would I get the `average` and `max` for both number values, `sales` and `cost`, for each `clerk`? How do I get those values by `region`? Or `region` and `store`?

For `clerk` stats:

It seems like I need to create a terms bucket for the clerks with their `sales` and `cost` sums. Then do a `stats_bucket` query over that bucket for each of the `sales` and `cost` numbers.

```auto
  "aggregations": {
    "clerkStats": {
      "terms": { "field": "clerk" },
      "aggs": {
        "sales": { "sum": { "field": "sales" } },
        "cost": { "sum": { "field": "cost" } }
      }
    },
    "salesStats": {
      "stats_bucket": { "buckets_path": "clerkStats>sales" }
    },
    "costStats": {
      "stats_bucket": { "buckets_path": "clerkStats>cost" }
    }
  }

```

This gives the following useful properties

- `salesStats.avg` would have the average `sales` for all `clerk`s
- `salesStats.max` would have the max `sales` for all `clerk`s
- `costStats.avg` would have the average `costs` for all `clerk`s
- `costStats.max` would have the max `costs` for all `clerk`s

To wrap that by `region` it would be

```auto
  "aggregations": {
    "region": {
      "terms": { "field": "region" },
      "aggs": {
        "aggs": {
          "clerkSums": {
            "terms": { "field": "clerk" },
            "aggs": {
              "sales": { "sum": { "field": "sales" } },
              "cost": { "sum": { "field": "cost" } }
            }
          },
          "salesStats": {
            "stats_bucket": { "buckets_path": "clerkSums>sales" }
          },
          "costStats": {
            "stats_bucket": { "buckets_path": "clerkSums>cost" }
          }
        }
      }
    }
  }

```

This gives the following useful properties in region buckets

- `salesStats.avg` being the average `sales` for all `clerk`s in that region
- `salesStats.max` being the max `sales` for all `clerk`s in that region
- `costStats.avg` being the average `cost` for all `clerk`s in that region
- `costStats.max` being the max `cost` for all `clerk`s in that region

For `region` and `store`:

Same thing as the `region` alone query, but in a composite query with both region and store?

```auto
  "aggregations": {
    "combo": {
      "composite": {
        "sources": [
          { "region": { "terms": { "field": "region" } } },
          { "store": { "terms": { "field": "store" } } }
        ]
      },
      "aggs": {
        "aggs": {
          "clerkSums": {
            "terms": { "field": "clerk" },
            "aggs": {
              "sales": { "sum": { "field": "sales" } },
              "cost": { "sum": { "field": "cost" } }
            }
          },
          "salesStats": {
            "stats_bucket": { "buckets_path": "clerkSums>sales" }
          },
          "costStats": {
            "stats_bucket": { "buckets_path": "clerkSums>cost" }
          }
        }
      }
    }
  }

```

This does not work. You cannot include the clerkSums inside the composite aggregation.

The only workaround I know of is to do 2 terms aggregations to nest region and store.

Question:

- How do I get the average sales the clerk's are doing in each region and store?
- How do I not include `clerkSums` in the responses? I only care about `salesStats` and `costStats`?

---

<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:** [March 26, 2019, 2:35am UTC](https://discuss.elastic.co/t/stats-bucket-the-correct-query-here/169931/2 "2019-03-26T02:35:00Z")

</div>

Closed on March 26, 2019, 2:35am UTC

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