# Same level script aggregation

**URL:** https://discuss.elastic.co/t/same-level-script-aggregation/278631
**Category:** Elasticsearch
**Created:** [July 14, 2021, 9:23am UTC](https://discuss.elastic.co/t/same-level-script-aggregation/278631 "2021-07-14T09:23:20Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Ivan\_Janev](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ivan_janev/32/87140_2.png) [@Ivan\_Janev](https://discuss.elastic.co/u/Ivan_Janev)
#### Post date: [July 14, 2021, 9:23am UTC](https://discuss.elastic.co/t/same-level-script-aggregation/278631/1 "2021-07-14T09:23:20Z")

</div>

I'm wondering if it is possible to do a same level script aggregation in ES. Let's say I have 2 fields in my documents `sales` and `spend`. This is the result I want to achieve:

```auto
{
  "size": 0,
  "query": {
    "match_all": {}
  },
  "aggs": {
    "sales": {
      "sum": {
        "field": "sales"
      }
    },
    "spend": {
      "sum": {
        "field": "spend"
      }
    },
    "custom_calulation": {
      // something that will calculate sales/spend
    }
  }
}

```

I know that I can easily do this using per say a `date_histogram` and use the `bucket_script` aggregation, so I'm trying to do same here only with the same level aggregation, so I don't have to nest them.

---

<div class="post-metadata">

### Author: ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)
#### Post date: [July 15, 2021, 12:44pm UTC](https://discuss.elastic.co/t/same-level-script-aggregation/278631/2 "2021-07-15T12:44:24Z")

</div>

I'm a little unsure about the question. As you know about the `bucket_script` agg, why didnt it work in your case? Maybe a fully reproducible example would help in this case.

---

<div class="post-metadata">

### Author: ![Ivan\_Janev](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ivan_janev/32/87140_2.png) [@Ivan\_Janev](https://discuss.elastic.co/u/Ivan_Janev)
#### Post date: [July 15, 2021, 1:16pm UTC](https://discuss.elastic.co/t/same-level-script-aggregation/278631/3 "2021-07-15T13:16:15Z")

</div>

So this is the original query:

```auto
{
  "size": 0,
  "query": {
    "bool": {
      "must": [
        {
          "range": {
            "report_date": {
              "gte": "2021-01-01",
              "lte": "2021-12-31"
            }
          }
        },
        {
          "bool": {
            "minimum_should_match": 1,
            "should": [
              {
                "term": {
                  "segment": {
                    "value": "null"
                  }
                }
              },
              {
                "bool": {
                  "must_not": [
                    {
                      "exists": {
                        "field": "segment"
                      }
                    }
                  ]
                }
              }
            ]
          }
        }
      ]
    }
  },
  "aggs": {
    "interval": {
      "date_histogram": {
        "field": "report_date",
        "interval": "year",
        "min_doc_count": 0,
        "extended_bounds": {
          "min": "2021-01-01",
          "max": "2021-12-31"
        }
      },
      "aggs": {
        "sales": {
          "sum": {
            "field": "attributed_sales_14d"
          }
        },
        "spend": {
          "sum": {
            "field": "cost"
          }
        },
        "clicks": {
          "sum": {
            "field": "clicks"
          }
        },
        "impressions": {
          "sum": {
            "field": "impressions"
          }
        },
        "roas": {
          "bucket_script": {
            "buckets_path": {
              "sales": "sales",
              "cost": "spend"
            },
            "script": "params.sales/params.cost",
            "gap_policy": "insert_zeros"
          }
        }
      }
    },
    "histogram_for_average": {
      "date_histogram": {
        "field": "report_date",
        "interval": "day"
      },
      "aggs": {
        "sales": {
          "sum": {
            "field": "attributed_sales_14d"
          }
        },
        "spend": {
          "sum": {
            "field": "cost"
          }
        },
        "roas": {
          "bucket_script": {
            "buckets_path": {
              "sales": "sales",
              "cost": "spend"
            },
            "script": "params.sales/params.cost",
            "gap_policy": "insert_zeros"
          }
        }
      }
    },
    "average_spend": {
      "avg_bucket": {
        "buckets_path": "histogram_for_average>spend"
      }
    },
    "average_sales": {
      "avg_bucket": {
        "buckets_path": "histogram_for_average>sales"
      }
    },
    "average_roas": {
      "avg_bucket": {
        "buckets_path": "histogram_for_average>roas"
      }
    }
  }
}

```

This first histogram is used to represent data and can be done with `year`, `day` and `month` values. The average that needs to be calculated must always be done with a `day` interval. What I want to achieve is to replace the `average_roas` with a calculation `average_sales/average_spend` .

To further elaborate on the average\_histogram. I have multiple documents for each day and the `sales` for one day is the sum of all the documents for that individual day. That's why I want to sum them daily and then get the daily average sales. I do the same for `spend` as well.

---

<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: [August 12, 2021, 1:17pm UTC](https://discuss.elastic.co/t/same-level-script-aggregation/278631/4 "2021-08-12T13:17:07Z")

</div>

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