# How to limit result in PPL with "stats count() by field" to 10

**URL:** <https://forum.opensearch.org/t/how-to-limit-result-in-ppl-with-stats-count-by-field-to-10/17102>\
**Category:** OpenSearch\
**Created:** [December 15, 2023, 7:15am UTC](https://forum.opensearch.org/t/how-to-limit-result-in-ppl-with-stats-count-by-field-to-10/17102 "2023-12-15T07:15:13Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Juergen](https://avatars.discourse-cdn.com/v4/letter/j/65b543/32.png) [@Juergen](https://forum.opensearch.org/u/Juergen)\
**Post date:** [December 15, 2023, 7:15am UTC](https://forum.opensearch.org/t/how-to-limit-result-in-ppl-with-stats-count-by-field-to-10/17102/1 "2023-12-15T07:15:13Z")

</div>

**Opensearch 2.11.0**

Helle there,

how could I limit with PPL the result of this statement:

source=dummyindey| stats count() by dummyfield

My goal is to get a Top-ten list with a count and name.  
To visaluize it with a Pie-chart or others.

Thanks a lot.

regards  
Juergen

---

<div class="post-metadata">

**Author:** ![gaobinlong](https://sea1.discourse-cdn.com/flex019/user_avatar/forum.opensearch.org/gaobinlong/32/5242_2.png) [@gaobinlong](https://forum.opensearch.org/u/gaobinlong)\
**Post date:** [December 16, 2023, 1:46pm UTC](https://forum.opensearch.org/t/how-to-limit-result-in-ppl-with-stats-count-by-field-to-10/17102/2 "2023-12-16T13:46:29Z")

</div>

You can use `head` to limit the results, `| head 10`.

---

<div class="post-metadata">

**Author:** ![Juergen](https://avatars.discourse-cdn.com/v4/letter/j/65b543/32.png) [@Juergen](https://forum.opensearch.org/u/Juergen)\
**Post date:** [December 19, 2023, 6:32am UTC](https://forum.opensearch.org/t/how-to-limit-result-in-ppl-with-stats-count-by-field-to-10/17102/3 "2023-12-19T06:32:47Z")

</div>

That returns the first 10 lines, but unfortunately unsortet.  
I need the ten lines with the highest “stats count() by dummyfield”

---

<div class="post-metadata">

**Author:** ![Juergen](https://avatars.discourse-cdn.com/v4/letter/j/65b543/32.png) [@Juergen](https://forum.opensearch.org/u/Juergen)\
**Post date:** [December 19, 2023, 6:41am UTC](https://forum.opensearch.org/t/how-to-limit-result-in-ppl-with-stats-count-by-field-to-10/17102/4 "2023-12-19T06:41:13Z")

</div>

Sorting works with the dummfield:

This works:

source=dummyindex| stats count() by dummyfield | sort dummyfield | head 10

but this doesn’t:

source=dummyindex| stats count() by dummyfield | sort count() | head 10

Is there a way to use count() in the sort?

Thanks a lot.

---

<div class="post-metadata">

**Author:** ![Juergen](https://avatars.discourse-cdn.com/v4/letter/j/65b543/32.png) [@Juergen](https://forum.opensearch.org/u/Juergen)\
**Post date:** [December 19, 2023, 7:03am UTC](https://forum.opensearch.org/t/how-to-limit-result-in-ppl-with-stats-count-by-field-to-10/17102/5 "2023-12-19T07:03:11Z")

</div>

I got it. Backticks are needed:

source = dummyindex | stats count() by dummyfield | sort - `count()` | head 10

Hope, it helps someone else : -)

Thanks.
