# Grouping using multiple fields

**URL:** <https://discuss.elastic.co/t/grouping-using-multiple-fields/48244>\
**Category:** Elasticsearch\
**Created:** [April 24, 2016, 12:13am UTC](https://discuss.elastic.co/t/grouping-using-multiple-fields/48244 "2016-04-24T00:13:26Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![chris\_es](https://avatars.discourse-cdn.com/v4/letter/c/977dab/32.png) [@chris\_es](https://discuss.elastic.co/u/chris_es)\
**Post date:** [April 24, 2016, 12:13am UTC](https://discuss.elastic.co/t/grouping-using-multiple-fields/48244/1 "2016-04-24T00:13:26Z")

</div>

The objective is to group by two fields and then sum the corresponding amounts. Equivalent to the SQL query:  
select empID, empName , SUM(salary) FROM random GROUP BY empId, empName.

I used terms aggregation but it does not seem to work correctly when I use two terms. Basically at the end I would like to have one dictionary with the key as (empId + empName) and the value as the summed salary. But my attempts have resulted in created a nested grouping with two levels and thus incorrect results.

I am using the NEST client for .net.

---

<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:56pm UTC](https://discuss.elastic.co/t/grouping-using-multiple-fields/48244/2 "2017-07-05T22:56:44Z")

</div>


