# X-pack sql - summation of case statement does not work

**URL:** <https://discuss.elastic.co/t/x-pack-sql-summation-of-case-statement-does-not-work/192727>\
**Category:** Elasticsearch\
**Created:** [July 29, 2019, 2:37pm UTC](https://discuss.elastic.co/t/x-pack-sql-summation-of-case-statement-does-not-work/192727 "2019-07-29T14:37:59Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![isurika\_lakmali](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/isurika_lakmali/32/51139_2.png) [@isurika\_lakmali](https://discuss.elastic.co/u/isurika_lakmali)\
**Post date:** [July 29, 2019, 2:37pm UTC](https://discuss.elastic.co/t/x-pack-sql-summation-of-case-statement-does-not-work/192727/1 "2019-07-29T14:37:59Z")

</div>

Hi Team,

I am trying to execute following sql query using x-pack sql.  
POST /\_sql  
{  
"query" :  
"select sum(case when metric\_name ='requirement' then 1 else 0 end) as req\_count ,sum(case when metric\_name ='defect' then 1 else 0 end) as defect\_count from my\_metric"  
}

But It throws following error.  
{  
"error": {  
"root\_cause": [  
{  
"type": "sql\_illegal\_argument\_exception",  
"reason": "Does not know how to convert argument Case for function Sum"  
}  
],  
"type": "sql\_illegal\_argument\_exception",  
"reason": "Does not know how to convert argument Case for function Sum"  
},  
"status": 500  
}

Appreciate if you can provide solution to overcome this error.

---

<div class="post-metadata">

**Author:** ![Andrei\_Stefan](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/andrei_stefan/32/47533_2.png) [@Andrei\_Stefan](https://discuss.elastic.co/u/Andrei_Stefan)\
**Post date:** [July 30, 2019, 9:59am UTC](https://discuss.elastic.co/t/x-pack-sql-summation-of-case-statement-does-not-work/192727/2 "2019-07-30T09:59:26Z")

</div>

@isurika_lakmali this type of query (an aggregation over a function) is not currently supported. There is a more general issue reported here: [https://github.com/elastic/elasticsearch/issues/36865](https://github.com/elastic/elasticsearch/issues/36865)

---

<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:** [August 27, 2019, 9:59am UTC](https://discuss.elastic.co/t/x-pack-sql-summation-of-case-statement-does-not-work/192727/3 "2019-08-27T09:59:27Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
