# Group by Aggregation doc\_count

**URL:** <https://discuss.elastic.co/t/group-by-aggregation-doc-count/152800>\
**Category:** Elasticsearch\
**Created:** [October 17, 2018, 10:16am UTC](https://discuss.elastic.co/t/group-by-aggregation-doc-count/152800 "2018-10-17T10:16:29Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![connor](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/connor/32/36606_2.png) [@connor](https://discuss.elastic.co/u/connor)\
**Post date:** [October 17, 2018, 10:16am UTC](https://discuss.elastic.co/t/group-by-aggregation-doc-count/152800/1 "2018-10-17T10:16:29Z")

</div>

Hi, I'm hoping someone could assist with this query.

I'm trying to group by the doc\_count from another aggregation.

In SQL this query would look like this:

```
SELECT bucket, COUNT(1) AS bucket_count FROM (
    SELECT a_field, COUNT(1) AS bucket FROM table GROUP BY a_field
)
GROUP BY bucket

```

So initial aggregation would look like this:

```
"aggs": {
	"group_by_a_field": {
		"terms": {
			"field": "a_field"
		}
	}
}

```

How do i then go about grouping on the respective doc\_count's?

Thanks in advance for any response!

---

<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:** [November 14, 2018, 10:16am UTC](https://discuss.elastic.co/t/group-by-aggregation-doc-count/152800/2 "2018-11-14T10:16:33Z")

</div>

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