Group by & sum field


(Ivan Ghisleni) #1

Hi to all,

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

thanks
Ivan


(David Pilato) #2

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


(Ivan Ghisleni) #3

It works Thank u!!


(Ivan Ghisleni) #4

Just another question is it possible order by do_a_sum_on_field_yyy ?


(David Pilato) #5

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

David


(Ivan Ghisleni) #6

Thanks


(system) #7