Group by & sum field

Hi to all,

I want to get the Elasticsearch translation of SQL "Group by & sum field" anyone can help me?

thanks
Ivan

1 Like

You can run a first level of aggregation (let say a Terms aggregation) and the add a sub sum aggregation.

Like:

{
  "aggs": {
    "by_xxx": {
      "terms": {
        "field": "xxx"
      },
      "aggs": {
       "do_a_sum_on_field_yyy":  {
          "sum": {
             "field": "yyy"
          }
       }
      }
    }
  }
}

Written from the top of my head so might be buggy though.

HTH

4 Likes

It works Thank u!!

Just another question is it possible order by do_a_sum_on_field_yyy ?

I don't think so. As the first level of the tree will be by term.

David

Thanks