# Scripting aggregation

**URL:** <https://discuss.elastic.co/t/scripting-aggregation/120682>\
**Category:** Elasticsearch\
**Created:** [February 20, 2018, 4:29pm UTC](https://discuss.elastic.co/t/scripting-aggregation/120682 "2018-02-20T16:29:32Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![lore6673](https://avatars.discourse-cdn.com/v4/letter/l/e56c9b/32.png) [@lore6673](https://discuss.elastic.co/u/lore6673)\
**Post date:** [February 20, 2018, 4:29pm UTC](https://discuss.elastic.co/t/scripting-aggregation/120682/1 "2018-02-20T16:29:33Z")

</div>

Hi  
I'm working to get some KPI on my index where I load the company sales and I would like to calculate the following

Avg Ticket = Total Sales / # Tickets

I'm using the following payload:

```
{
  "size": 0,
  "aggs": {
    "tot_amount_euro": {
      "sum": {
        "field": "amt_euro"
      }
    },
    "count_ticket": {
      "cardinality": {
        "field": "ticket_code.keyword"
      }
    }
  }
}

```

and I'm getting the values to calculate the the KPI, but I would like to have the value directly from ES and then I'm trying to use the scripting bucket:

```
{
  "size": 0,
  "aggs": {
    "tot_amount_euro": {
      "sum": {
        "field": "amt_euro"
      }
    },
    "count_ticket": {
      "cardinality": {
        "field": "ticket_code.keyword"
      },
      "avg_ticket": {
        "bucket_script": {
          "buckets_path": {
            "tSales": "tot_amtount_euro",
            "tQty": "count_ticket"
          },
          "script": "params.tSales / params.tQty"
        }
      }
    }
  }
}

```

but I got this error:

"Found two aggregation type definitions in [count\_ticket]: [cardinality] and [avg\_ticket]"

Any suggestion ?

Thanks

---

<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 20, 2018, 4:29pm UTC](https://discuss.elastic.co/t/scripting-aggregation/120682/2 "2018-03-20T16:29:35Z")

</div>

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