# How to find difference between aggregate average and individual values

**URL:** <https://discuss.elastic.co/t/how-to-find-difference-between-aggregate-average-and-individual-values/330255>\
**Category:** Elasticsearch\
**Tags:** painless, eql-elastic-query-language\
**Created:** [April 18, 2023, 10:56pm UTC](https://discuss.elastic.co/t/how-to-find-difference-between-aggregate-average-and-individual-values/330255 "2023-04-18T22:56:25Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![rahulkothanath](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rahulkothanath/32/99122_2.png) [@rahulkothanath](https://discuss.elastic.co/u/rahulkothanath)\
**Post date:** [April 18, 2023, 10:56pm UTC](https://discuss.elastic.co/t/how-to-find-difference-between-aggregate-average-and-individual-values/330255/1 "2023-04-18T22:56:26Z")

</div>

I have a field "y" in the Elasticsearch index and I would like to find sum(y-y.avg) using aggregations for each person in my index. I am able to calculate y.avg for a person using the below query but need help in calculating sum(y-y.avg).

```auto
GET credit_score_transform_v1/_search
{
  "query": {
    "match": {
      "person": "joy"
    }
  },
  "size": 0, 
  "aggs": {
      "avg_y": {
        "avg": {
          "field": "y"
        }
      },
      "y-y.bar": {
        "bucket_script": {
          "buckets_path": {
            "y_bar": "avg_y.value"
          },
          "script": {
            "source": "params.y_bar"
          }
        }
      }
  }
}

```

I am able to access "y.avg" using the bucket path and need help in calculating the sum(y-y.avg).

thanks for the help and support.

---

<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:** [May 16, 2023, 10:57pm UTC](https://discuss.elastic.co/t/how-to-find-difference-between-aggregate-average-and-individual-values/330255/2 "2023-05-16T22:57:14Z")

</div>

Closed on May 16, 2023, 10:57pm UTC

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