# Need query to filter two field and get the unique count bases on one field

**URL:** <https://discuss.elastic.co/t/need-query-to-filter-two-field-and-get-the-unique-count-bases-on-one-field/220129>\
**Category:** Kibana\
**Created:** [February 20, 2020, 8:38am UTC](https://discuss.elastic.co/t/need-query-to-filter-two-field-and-get-the-unique-count-bases-on-one-field/220129 "2020-02-20T08:38:18Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Prateek\_Bajaj](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/prateek_bajaj/32/49330_2.png) [@Prateek\_Bajaj](https://discuss.elastic.co/u/Prateek_Bajaj)\
**Post date:** [February 20, 2020, 8:38am UTC](https://discuss.elastic.co/t/need-query-to-filter-two-field-and-get-the-unique-count-bases-on-one-field/220129/1 "2020-02-20T08:38:18Z")

</div>

Hi , I want to write a \_search or \_count query (kql) that will give the unique count based on one field after filtering two other fields. It will be really helpful if you guys can help me with it.

---

<div class="post-metadata">

**Author:** ![flash1293](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/flash1293/32/41227_2.png) [@flash1293](https://discuss.elastic.co/u/flash1293)\
**Post date:** [February 20, 2020, 10:33am UTC](https://discuss.elastic.co/t/need-query-to-filter-two-field-and-get-the-unique-count-bases-on-one-field/220129/2 "2020-02-20T10:33:02Z")

</div>

Hi @Prateek_Bajaj,

KQL is a language to filter down the set of documents of the current view, it can't help you with aggregating these documents (e.g. by doing a unique count on them). Think of it as just the `WHERE` clause of a SQL query. For this you need to add a visualization to the mix.

Check out the following example using a metric aggregation:

 ![Screenshot 2020-02-20 at 11.29.59](https://us1.discourse-cdn.com/elastic/original/3X/a/f/afec52f3482fe0d0bee8a0ae051f3d807de48334.png)

The query in the KQL bar is filtering down the documents based on two different fields, the configured metric is applying the "Unique Count" aggregation based on a specified third field.

You can save this visualization and put it on a dashboard - the content of the query bar will be saved along with the Visualization itself.

---

<div class="post-metadata">

**Author:** ![Prateek\_Bajaj](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/prateek_bajaj/32/49330_2.png) [@Prateek\_Bajaj](https://discuss.elastic.co/u/Prateek_Bajaj)\
**Post date:** [February 20, 2020, 10:43am UTC](https://discuss.elastic.co/t/need-query-to-filter-two-field-and-get-the-unique-count-bases-on-one-field/220129/3 "2020-02-20T10:43:39Z")

</div>

Hi @flash1293  
Thanks alot for the reply, It helped me alot. Can I create a \_search call to elastic search to get this final number as response. ??

---

<div class="post-metadata">

**Author:** ![flash1293](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/flash1293/32/41227_2.png) [@flash1293](https://discuss.elastic.co/u/flash1293)\
**Post date:** [February 20, 2020, 11:05am UTC](https://discuss.elastic.co/t/need-query-to-filter-two-field-and-get-the-unique-count-bases-on-one-field/220129/4 "2020-02-20T11:05:22Z")

</div>

If you click the `Inspect` button in the top nav, you can see the request and response made by the visualization based on this configuration.

---

<div class="post-metadata">

**Author:** ![Prateek\_Bajaj](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/prateek_bajaj/32/49330_2.png) [@Prateek\_Bajaj](https://discuss.elastic.co/u/Prateek_Bajaj)\
**Post date:** [February 20, 2020, 1:03pm UTC](https://discuss.elastic.co/t/need-query-to-filter-two-field-and-get-the-unique-count-bases-on-one-field/220129/5 "2020-02-20T13:03:52Z")

</div>

Thanks alot , This is exactly what I was looking for.

---

<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 19, 2020, 1:11pm UTC](https://discuss.elastic.co/t/need-query-to-filter-two-field-and-get-the-unique-count-bases-on-one-field/220129/6 "2020-03-19T13:11:01Z")

</div>

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