# Returning several fields with aggregation

**URL:** <https://discuss.elastic.co/t/returning-several-fields-with-aggregation/54746>\
**Category:** Elasticsearch\
**Created:** [July 5, 2016, 2:29pm UTC](https://discuss.elastic.co/t/returning-several-fields-with-aggregation/54746 "2016-07-05T14:29:33Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![yuvalz](https://avatars.discourse-cdn.com/v4/letter/y/b19c9b/32.png) [@yuvalz](https://discuss.elastic.co/u/yuvalz)\
**Post date:** [July 5, 2016, 2:29pm UTC](https://discuss.elastic.co/t/returning-several-fields-with-aggregation/54746/1 "2016-07-05T14:29:33Z")

</div>

I want to make an aggregation of `sum` but also return the "group by" fields.

For example, i want to make a query on a school database, to get the average grade on a specific class and study  
in SQL i run:  
`select class, study, examId, avg(grade) from grades group by study, examId`  
and the result is something like:  
`class | study | avg(grade) a1 | gymnastics | 85 a1 | mathematics | 87 a2 | gymnastics | 85 a2 | mathematics | 87`

so the result contains the group by fields (class and study) and the aggregation result.

I want to make the same query with elastic, and to return all the columns in an aggregation (class, study, avg(grade))

Any idea?  
Thanks in advance.  
Yuval

---

<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:** [July 5, 2017, 10:37pm UTC](https://discuss.elastic.co/t/returning-several-fields-with-aggregation/54746/2 "2017-07-05T22:37:56Z")

</div>


