# SQL Query with CASE When condition and SUM

**URL:** <https://discuss.elastic.co/t/sql-query-with-case-when-condition-and-sum/217397>\
**Category:** Elasticsearch\
**Created:** [January 31, 2020, 2:28pm UTC](https://discuss.elastic.co/t/sql-query-with-case-when-condition-and-sum/217397 "2020-01-31T14:28:22Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Zou\_Justin](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/zou_justin/32/61852_2.png) [@Zou\_Justin](https://discuss.elastic.co/u/Zou_Justin)\
**Post date:** [January 31, 2020, 2:28pm UTC](https://discuss.elastic.co/t/sql-query-with-case-when-condition-and-sum/217397/1 "2020-01-31T14:28:22Z")

</div>

Hi Experts,

Can we use the case when with SUM in one SQL in Elastic Search7.5.1? Does ES support this in SQL or other way? I tried to do this , while there have following issue:  
sql\> select sum(case when model\_qi\_category=0 then 1 else 0 end) FROM es\_voice\_model\_result where tenant\_id = 2 group by model\_qi\_category;  
Server error [Server encountered an error [Does not know how to convert argument Case for function Sum]. [SqlIllegalArgumentException[Does not know how to convert argument Case for function Sum]  
at org.elasticsearch.xpack.sql.planner.QueryTranslator.field(QueryTranslator.java:500)  
at org.elasticsearch.xpack.sql.planner.QueryTranslator$Sums.toAgg(QueryTranslator.java:884)

Thanks

---

<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:** [February 4, 2020, 8:58am UTC](https://discuss.elastic.co/t/sql-query-with-case-when-condition-and-sum/217397/2 "2020-02-04T08:58:37Z")

</div>

Hi @Zou_Justin,  
That type of query is not supported at the moment. But there is a github issue for it: [https://github.com/elastic/elasticsearch/issues/37271](https://github.com/elastic/elasticsearch/issues/37271)

---

<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:** [March 3, 2020, 8:58am UTC](https://discuss.elastic.co/t/sql-query-with-case-when-condition-and-sum/217397/3 "2020-03-03T08:58:59Z")

</div>

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