0

How can I write this in elasticsearch:

    SELECT avg(sum) 
    FROM (
         SELECT sum(rev) as sum
         FROM table
         GROUP BY user_id
    )

1 Answer 1

0

If ES version you are using is 2.x use Avg Bucket Aggregation

{

"aggs": {
  "group_by_user": {
     "terms": {
        "field": "userId"
     },
     "aggs": {
        "rev_sum": {
           "sum": {
              "field": "rev"
           }
        }
     }
  },
  "avg_monthly_sales": {
     "avg_bucket": {
        "buckets_path": "group_by_user>rev_sum"
       }
     }
   }
 }

Hope it helps

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.