# Order or Sort Serial\_diff Agg result

**URL:** <https://discuss.elastic.co/t/order-or-sort-serial-diff-agg-result/223647>\
**Category:** Elasticsearch\
**Created:** [March 15, 2020, 7:31am UTC](https://discuss.elastic.co/t/order-or-sort-serial-diff-agg-result/223647 "2020-03-15T07:31:15Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![ad1982](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ad1982/32/45433_2.png) [@ad1982](https://discuss.elastic.co/u/ad1982)\
**Post date:** [March 15, 2020, 7:31am UTC](https://discuss.elastic.co/t/order-or-sort-serial-diff-agg-result/223647/1 "2020-03-15T07:31:15Z")

</div>

I am trying to sort the result of a serial\_diff agg. and struggling with that. Below is the code

```auto
ET db/_search
{
    "size": 0,
    "query": {
        "bool": {
            "must": [
                {
                    "terms": {
                        "Name": [
                            "q"
                        ]
                    }
                }
            ],
            "filter": [
                {
                    "range": {
                        "ts": {
                            "gte": "2020-03-09T09:00:00.000Z",
                            "lte": "2020-03-09T12:40:00.000Z",
                            "format": "date_optional_time"
                        }
                    }
                }
            ]
        }
    },
    "aggs": {
        "sourceNameCount": {
            "cardinality": {
                "field": "sourceName"
            }
        },
        "sourceName": {
            "terms": {
                "size": 100,
                "field": "sourceName"
            },
            "aggs": {
                "timeseries": {
                    "date_histogram": {
                        "field": "ts",
                        "min_doc_count": 1,
                        "interval": "15m",
                        "order": {
                          "_key": "asc"
                        }

                    },
                    "aggs": {
                      "the_sum":{
                        "avg":{
                            "field": "libVal"
                        }
                      },
                      "ts_diff":{
                        "serial_diff": {
                          "buckets_path": "the_sum",
                          "lag": 1
                        }
                      }
                    }
                }
              }
          }
      }
  }

```

---

<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:** [April 12, 2020, 7:31am UTC](https://discuss.elastic.co/t/order-or-sort-serial-diff-agg-result/223647/2 "2020-04-12T07:31:31Z")

</div>

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