# Query to divide aggregation result by a parent value

**URL:** <https://discuss.elastic.co/t/query-to-divide-aggregation-result-by-a-parent-value/244552>\
**Category:** Elasticsearch\
**Created:** [August 11, 2020, 12:52pm UTC](https://discuss.elastic.co/t/query-to-divide-aggregation-result-by-a-parent-value/244552 "2020-08-11T12:52:48Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jong99](https://avatars.discourse-cdn.com/v4/letter/j/9dc877/32.png) [@jong99](https://discuss.elastic.co/u/jong99)\
**Post date:** [August 11, 2020, 12:52pm UTC](https://discuss.elastic.co/t/query-to-divide-aggregation-result-by-a-parent-value/244552/1 "2020-08-11T12:52:48Z")

</div>

Say I have a series of documents with an **event** type and a **user\_id**. Is there an Elasticsearch pipeline aggregation that allows me to divide the count of events by the unique total users (not total users per event)? I know I can do this in timelion but trying to craft a query to do this. Any pointers?

Here is what I have so far:

```auto
{
  "aggs": {
    "event_count": {
      "terms": {
        "field": "event",
        "size": 5,
        "order": {
          "_count": "desc"
        }
      }
    },
    "num_users": {
      "cardinality": {
        "field": "user_id"
      }
    }
  },
  "size": 0
}

```

I want to divide each of the **event\_count** values by **num\_users**. Is that possible? I've tried a bucket script aggregation but not sure how I would work the paths out.

Hope I have explained that clearly enough! Thanks 🙂

---

<div class="post-metadata">

**Author:** ![jong99](https://avatars.discourse-cdn.com/v4/letter/j/9dc877/32.png) [@jong99](https://discuss.elastic.co/u/jong99)\
**Post date:** [August 11, 2020, 4:14pm UTC](https://discuss.elastic.co/t/query-to-divide-aggregation-result-by-a-parent-value/244552/2 "2020-08-11T16:14:20Z")

</div>

Just been looking at the documentation for bucket scripts, which says:

> the path cannot go back "up" the aggregation tree.

So I definitely can't place a bucket script inside `event_count` as I need access to `num_users` above it. But I can't place one next to `num_users` as I need to do this for _all_ values in the `event_count` array.

Is this just not possible using Elasticsearch?

---

<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:** [September 8, 2020, 4:14pm UTC](https://discuss.elastic.co/t/query-to-divide-aggregation-result-by-a-parent-value/244552/3 "2020-09-08T16:14:20Z")

</div>

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