# Lens or Aggs query to group by having count \> X

**URL:** <https://discuss.elastic.co/t/lens-or-aggs-query-to-group-by-having-count-x/354264>\
**Category:** Kibana\
**Created:** [February 27, 2024, 6:18pm UTC](https://discuss.elastic.co/t/lens-or-aggs-query-to-group-by-having-count-x/354264 "2024-02-27T18:18:50Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![qd-danh](https://avatars.discourse-cdn.com/v4/letter/q/43a26b/32.png) [@qd-danh](https://discuss.elastic.co/u/qd-danh)\
**Post date:** [February 27, 2024, 6:18pm UTC](https://discuss.elastic.co/t/lens-or-aggs-query-to-group-by-having-count-x/354264/1 "2024-02-27T18:18:50Z")

</div>

I'm drawing a blank on how to accomplish this in Kibana or Elastic.  
Prefer to have this in a Lens visual so I can add to a dashboard, but minimally I can do it with an aggregate API query in Kibana dev tools.

I'm dumbing down my documents to something super simple for the example.

```auto
[
  {
    "parentID": 123,
    "score": 65
  },
  {
    "parentID": 123,
    "score": 45
  },
  {
    "parentID": 123,
    "score": 94
  },
  {
    "parentID": 456,
    "score": 87
  },
  {
    "parentID": 456,
    "score": 67
  }
]

```

What I need to see is group by parentID and all the counts, then only see those with counts \> 2.

In SQL it would be

```auto
select parentID, count(*) 
from docs
group by parentID
having count(*) > 2

```

I think I'm close with this aggs query, but I don't know how to limit to only those with count \> 2

```auto
GET test-data/_search
{
  "size": 0,
  "aggs" : {
    "parents" : {
      "terms" : {
        "field" : "parentID",
        "size": 400
      }
    }
  }
}

```

Lastly, is there any way to do this same thing in a Lens visualization?  
I struggle to translate these API calls to something in Lens.

Thank you!

---

<div class="post-metadata">

**Author:** ![stephenb](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/stephenb/32/40856_2.png) [@stephenb](https://discuss.elastic.co/u/stephenb)\
**Post date:** [February 27, 2024, 8:54pm UTC](https://discuss.elastic.co/t/lens-or-aggs-query-to-group-by-having-count-x/354264/2 "2024-02-27T20:54:49Z")

</div>

@qd What version are you on?

---

<div class="post-metadata">

**Author:** ![qd-danh](https://avatars.discourse-cdn.com/v4/letter/q/43a26b/32.png) [@qd-danh](https://discuss.elastic.co/u/qd-danh)\
**Post date:** [February 27, 2024, 9:07pm UTC](https://discuss.elastic.co/t/lens-or-aggs-query-to-group-by-having-count-x/354264/3 "2024-02-27T21:07:04Z")

</div>

version 8.11

---

<div class="post-metadata">

**Author:** ![stephenb](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/stephenb/32/40856_2.png) [@stephenb](https://discuss.elastic.co/u/stephenb)\
**Post date:** [February 27, 2024, 9:25pm UTC](https://discuss.elastic.co/t/lens-or-aggs-query-to-group-by-having-count-x/354264/4 "2024-02-27T21:25:01Z")

</div>

You should try ESQL 🙂 Pretty sure you can do that.

> **[Using ES|QL in Kibana | Elasticsearch Guide \[8.11\] | Elastic](https://www.elastic.co/guide/en/elasticsearch/reference/8.11/esql-kibana.html)**

I do think you can do that with lens ... I would have to try...

So in the above example only parent ID and account for number 123 would be returned. Is that correct?

---

<div class="post-metadata">

**Author:** ![qd-danh](https://avatars.discourse-cdn.com/v4/letter/q/43a26b/32.png) [@qd-danh](https://discuss.elastic.co/u/qd-danh)\
**Post date:** [February 27, 2024, 10:20pm UTC](https://discuss.elastic.co/t/lens-or-aggs-query-to-group-by-having-count-x/354264/5 "2024-02-27T22:20:26Z")

</div>

Ooooh - ES|QL - good point. I forgot about that and haven't used it yet. Will try. Thank you.

Yes that's correct. I would like the `parentID` and the COUNT of records for that value (in this case 3).  
When you extend this across a large data set (and lots more data in each document) I'm still just wanting the `parentID` values that have more than 2 documents.

---

<div class="post-metadata">

**Author:** ![qd-danh](https://avatars.discourse-cdn.com/v4/letter/q/43a26b/32.png) [@qd-danh](https://discuss.elastic.co/u/qd-danh)\
**Post date:** [March 6, 2024, 8:05pm UTC](https://discuss.elastic.co/t/lens-or-aggs-query-to-group-by-having-count-x/354264/6 "2024-03-06T20:05:23Z")

</div>

Just posting the solution to this in case anyone else needs it.

The ES|QL suggestion was great - thank you @stephenb.

```auto
from docs
| stats numResults = COUNT(*) by parentID

```

I kind of hacked my way through a Lens visual for this. Only shows the last bunch of results so it's not a complete solution. I may be missing some other visual that would show it better.

- vertical bar stacked
- horizontal axis: `parentID`
  - number values: big enough to show all the groups (my real data has 500+ of these)
  - rank ascending
  - group remaining as other is OFF

- vertical axis: count
- breakdown by: `parentID`

Some drawbacks of this visual:

- only shows highest or lowest N groups depending on rank direction
- have to guess at the number of groups to show, so might not see all the results

Thanks again for the help

---

<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:** [April 3, 2024, 8:05pm UTC](https://discuss.elastic.co/t/lens-or-aggs-query-to-group-by-having-count-x/354264/7 "2024-04-03T20:05:40Z")

</div>

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