# Group by with count distinct on a string field

**URL:** <https://discuss.elastic.co/t/group-by-with-count-distinct-on-a-string-field/13616>\
**Category:** Elasticsearch\
**Created:** [September 16, 2013, 9:22am UTC](https://discuss.elastic.co/t/group-by-with-count-distinct-on-a-string-field/13616 "2013-09-16T09:22:42Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Fabien\_Guiraud](https://avatars.discourse-cdn.com/v4/letter/f/57b2e6/32.png) [@Fabien\_Guiraud](https://discuss.elastic.co/u/Fabien_Guiraud)\
**Post date:** [September 16, 2013, 9:22am UTC](https://discuss.elastic.co/t/group-by-with-count-distinct-on-a-string-field/13616/1 "2013-09-16T09:22:42Z")

</div>

Hi,

I have a big problem with ES : I want to do a group by and I want to count  
distinct on a value (string value)

I tried with TermStats but it seems it doesn't support string as value...

any idea?

If it can help, my model :

## table

field\_id  
field\_a (string)  
field\_b (array of ids)  
field\_c (count)

In a SQL way, I want : "SELECT sum(field\_c), COUNT(DISTINCT(field\_a)) FROM  
table GROUP BY field\_b

I have no problem with SUM(field\_c) with TermStats, but impossible to do  
the same thing with a string value, I have an error : 'field\_a is not a  
number'

Thanks

--  
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).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<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, 2:16am UTC](https://discuss.elastic.co/t/group-by-with-count-distinct-on-a-string-field/13616/2 "2017-07-06T02:16:22Z")

</div>


