# Division between two sum fields and order by the result

**URL:** <https://discuss.elastic.co/t/division-between-two-sum-fields-and-order-by-the-result/94049>\
**Category:** Elasticsearch\
**Created:** [July 21, 2017, 3:14am UTC](https://discuss.elastic.co/t/division-between-two-sum-fields-and-order-by-the-result/94049 "2017-07-21T03:14:38Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![junkainiu](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/junkainiu/32/23928_2.png) [@junkainiu](https://discuss.elastic.co/u/junkainiu)\
**Post date:** [July 21, 2017, 3:14am UTC](https://discuss.elastic.co/t/division-between-two-sum-fields-and-order-by-the-result/94049/1 "2017-07-21T03:14:39Z")

</div>

Hi,  
I need help to order by the division between two sum fields, how can I do ? Something like this

```auto
{
  "aggs": {
    "field1": {
      "terms": { 
        "field": "field1",
        "size":50,
        "order": division
      },
      "aggs":{
        "field2":{
          "sum":{
            "field":"field2"
          }
        },
        "field3":{
          "sum":{
            "field":"field3"
          }
        },
        "division": {
          "bucket_script": {
            "buckets_path": {
               "my_var1": "field2",
               "my_var2": "field3"
             },
            "script": "params.my_var1 / params.my_var2"
           }
         }
      }
    }
  }
}

```

This aggregation actually dose not work because the [pipeline aggregation cannot be used for order](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-terms-aggregation.html), so how can I do ?

---

<div class="post-metadata">

**Author:** ![jpountz](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jpountz/32/45836_2.png) [@jpountz](https://discuss.elastic.co/u/jpountz)\
**Post date:** [July 21, 2017, 1:45pm UTC](https://discuss.elastic.co/t/division-between-two-sum-fields-and-order-by-the-result/94049/2 "2017-07-21T13:45:58Z")

</div>

It is not possible.

---

<div class="post-metadata">

**Author:** ![junkainiu](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/junkainiu/32/23928_2.png) [@junkainiu](https://discuss.elastic.co/u/junkainiu)\
**Post date:** [July 25, 2017, 3:10am UTC](https://discuss.elastic.co/t/division-between-two-sum-fields-and-order-by-the-result/94049/3 "2017-07-25T03:10:58Z")

</div>

Are there any other work arounds to make this possible? like using script?

---

<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 22, 2017, 3:11am UTC](https://discuss.elastic.co/t/division-between-two-sum-fields-and-order-by-the-result/94049/4 "2017-08-22T03:11:05Z")

</div>

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