# How to perform Sum of a field to a group by data

**URL:** <https://discuss.elastic.co/t/how-to-perform-sum-of-a-field-to-a-group-by-data/23041>\
**Category:** Elasticsearch\
**Created:** [April 1, 2015, 3:06pm UTC](https://discuss.elastic.co/t/how-to-perform-sum-of-a-field-to-a-group-by-data/23041 "2015-04-01T15:06:43Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Anusha](https://avatars.discourse-cdn.com/v4/letter/a/c5a1d2/32.png) [@Anusha](https://discuss.elastic.co/u/Anusha)\
**Post date:** [April 1, 2015, 3:06pm UTC](https://discuss.elastic.co/t/how-to-perform-sum-of-a-field-to-a-group-by-data/23041/1 "2015-04-01T15:06:43Z")

</div>

Hi Team,

I would like to get the exact result of an SQL query

select sum(Amount) from Emp group by (Name).... Here If I take an example  
like

id Name amount  
1 A1 100  
2 A2 200  
3 A3 300  
4 A1 200  
5 A2 300

So for the above query the result will be Sum(Amount) by grouping the names  
A1, A2, A3 and for A1--\> sum(100+200=300), A2--\>sum(200+300=500),  
A3--\>sum(300)...

For this query I have used the following equivalent query using  
Aggregations as shown below:

GET ABCD/\_search  
{  
"size": 0,

"aggs": {  
"group\_trans": {  
"terms": {  
"field": "trans"  
},  
"aggs": {  
"sum\_ordamt":{  
"sum":{  
"field":"ordamt"  
}  
}  
}  
}  
}  
}

Am getting the result as:

"aggregations": {  
"group\_trans": {  
"doc\_count\_error\_upper\_bound": 81,  
"sum\_other\_doc\_count": 1371988,  
"buckets": [  
{  
"key": "7ce2d738-5549-4da2-aed0-db939f3a9188",  
"doc\_count": 282,  
"sum\_ordamt": {  
"value": 0  
}  
},  
{  
"key": "8f4b187a-f321-4d25-bbc0-a9e78f5810b8",  
"doc\_count": 230,  
"sum\_ordamt": {  
"value": 0  
}  
},  
{  
"key": "ca54db4c-620b-4650-bef6-350e83a0ca2c",  
"doc\_count": 170,  
"sum\_ordamt": {  
"value": 0  
}  
}  
.  
.  
.  
]  
}  
}  
} .........................Here Am getting sum  
value==0, May I know is there any way to Make a query relevant to the SQL  
query given above for to give the sum depending on the group by names...

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/ab20bcf5-d573-431b-bb9e-7c1207109988%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/ab20bcf5-d573-431b-bb9e-7c1207109988%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![jpountz](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jpountz/32/45836_2.png) [@jpountz](https://discuss.elastic.co/u/jpountz)\
**Post date:** [April 1, 2015, 10:07pm UTC](https://discuss.elastic.co/t/how-to-perform-sum-of-a-field-to-a-group-by-data/23041/2 "2015-04-01T22:07:47Z")

</div>

Your request looks good. Did you maybe mis-type the field name for the sum  
aggregation?

On Wed, Apr 1, 2015 at 5:06 PM, Muddadi Hemaanusha \<  
[hemaanusha.bunny@gmail.com](mailto:hemaanusha.bunny@gmail.com)\> wrote:

> Hi Team,
> 
> I would like to get the exact result of an SQL query
> 
> select sum(Amount) from Emp group by (Name).... Here If I take an example  
> like
> 
> id Name amount  
> 1 A1 100  
> 2 A2 200  
> 3 A3 300  
> 4 A1 200  
> 5 A2 300
> 
> So for the above query the result will be Sum(Amount) by grouping the  
> names A1, A2, A3 and for A1--\> sum(100+200=300), A2--\>sum(200+300=500),  
> A3--\>sum(300)...
> 
> For this query I have used the following equivalent query using  
> Aggregations as shown below:
> 
> GET ABCD/\_search  
> {  
> "size": 0,
> 
> "aggs": {  
> "group\_trans": {  
> "terms": {  
> "field": "trans"  
> },  
> "aggs": {  
> "sum\_ordamt":{  
> "sum":{  
> "field":"ordamt"  
> }  
> }  
> }  
> }  
> }  
> }
> 
> Am getting the result as:
> 
> "aggregations": {  
> "group\_trans": {  
> "doc\_count\_error\_upper\_bound": 81,  
> "sum\_other\_doc\_count": 1371988,  
> "buckets": [  
> {  
> "key": "7ce2d738-5549-4da2-aed0-db939f3a9188",  
> "doc\_count": 282,  
> "sum\_ordamt": {  
> "value": 0  
> }  
> },  
> {  
> "key": "8f4b187a-f321-4d25-bbc0-a9e78f5810b8",  
> "doc\_count": 230,  
> "sum\_ordamt": {  
> "value": 0  
> }  
> },  
> {  
> "key": "ca54db4c-620b-4650-bef6-350e83a0ca2c",  
> "doc\_count": 170,  
> "sum\_ordamt": {  
> "value": 0  
> }  
> }  
> .  
> .  
> .  
> ]  
> }  
> }  
> } .........................Here Am getting sum  
> value==0, May I know is there any way to Make a query relevant to the SQL  
> query given above for to give the sum depending on the group by names...
> 
> --  
> You received this message because you are subscribed to the Google Groups  
> "elasticsearch" group.  
> To unsubscribe from this group and stop receiving emails from it, send an  
> email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
> To view this discussion on the web visit  
> [https://groups.google.com/d/msgid/elasticsearch/ab20bcf5-d573-431b-bb9e-7c1207109988%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/ab20bcf5-d573-431b-bb9e-7c1207109988%40googlegroups.com)  
> [https://groups.google.com/d/msgid/elasticsearch/ab20bcf5-d573-431b-bb9e-7c1207109988%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/ab20bcf5-d573-431b-bb9e-7c1207109988%40googlegroups.com?utm_medium=email&utm_source=footer)  
> .  
> For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

--  
Adrien

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/CAO5%3DkAhu6roECh%2BkkPROFqYWNoD6B\_nvn-79AEGDD6-FwcvAtA%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAO5%3DkAhu6roECh%2BkkPROFqYWNoD6B_nvn-79AEGDD6-FwcvAtA%40mail.gmail.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<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 6, 2017, 12:22am UTC](https://discuss.elastic.co/t/how-to-perform-sum-of-a-field-to-a-group-by-data/23041/3 "2017-07-06T00:22:10Z")

</div>


