# Aggregate query count having count

**URL:** <https://discuss.elastic.co/t/aggregate-query-count-having-count/206769>\
**Category:** Elasticsearch\
**Created:** [November 6, 2019, 10:06am UTC](https://discuss.elastic.co/t/aggregate-query-count-having-count/206769 "2019-11-06T10:06:38Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Petr.Simik](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/petr.simik/32/38082_2.png) [@Petr.Simik](https://discuss.elastic.co/u/Petr.Simik)\
**Post date:** [November 6, 2019, 10:06am UTC](https://discuss.elastic.co/t/aggregate-query-count-having-count/206769/1 "2019-11-06T10:06:38Z")

</div>

I need to create visualisation 4 lines  
(I want to identify how many customers in time are using how many devices)  
count (unique customer) having count(uniqued deviceid) = 2  
count (unique customer) having count(uniqued deviceid) = 3  
count (unique customer) having count(uniqued deviceid) = 4  
count (unique customer) having count(uniqued deviceid) \> 4

In Index I have 3 field  
@timestamp - date  
customer - keyword  
deviceid - keyword

this is a log from application, one customer can have several documents with same deviceid. This

How I can achieve this ?  
thank you

---

<div class="post-metadata">

**Author:** ![Petr.Simik](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/petr.simik/32/38082_2.png) [@Petr.Simik](https://discuss.elastic.co/u/Petr.Simik)\
**Post date:** [November 7, 2019, 11:44am UTC](https://discuss.elastic.co/t/aggregate-query-count-having-count/206769/2 "2019-11-07T11:44:55Z")

</div>

THis is what I am looking for

is there a way how to achieve this in elasticsearch?

count (unique customer)  
group by count(unique device\_id)

---

<div class="post-metadata">

**Author:** ![Mark\_Harwood](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood/32/10538_2.png) [@Mark\_Harwood](https://discuss.elastic.co/u/Mark_Harwood)\
**Post date:** [November 12, 2019, 3:26pm UTC](https://discuss.elastic.co/t/aggregate-query-count-having-count/206769/3 "2019-11-12T15:26:58Z")

</div>

This is sort of behavioural analysis will be hard to do using a distributed index on a lot of data. It requires a lot of joins based on a customer key and distributed joins are expensive in any system.

You'll likely need to build an "entity centric" index (each doc = one customer).  
The new [transform api](https://www.elastic.co/guide/en/elasticsearch/reference/current/transforms.html) can help collapse the device id for each customer doc using a `cardinality` aggregation. Once you've built the customer index you can use a date histogram in Kibana on it and and plot the lines using custom ranges for the 4 device ownership ranges you listed.

However, the challenge here is the date info - presumably you want that a customer could appear once in the line for February with 2 devices but again in March with 3 devices? This would mean the docs would not be `customer` documents but `customer-as-at` documents which summarise the person's device count on that particular date. That will likely require custom script to build that sort of index.

---

<div class="post-metadata">

**Author:** ![Petr.Simik](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/petr.simik/32/38082_2.png) [@Petr.Simik](https://discuss.elastic.co/u/Petr.Simik)\
**Post date:** [November 12, 2019, 3:43pm UTC](https://discuss.elastic.co/t/aggregate-query-count-having-count/206769/4 "2019-11-12T15:43:32Z")

</div>

Great I did not know about transform api , I am looking at documentation  
this seems to be useful in my case.  
I will give it a try. I hope I will be able to create code to transform the data for further analysis.

---

<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:** [December 10, 2019, 3:50pm UTC](https://discuss.elastic.co/t/aggregate-query-count-having-count/206769/5 "2019-12-10T15:50:57Z")

</div>

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