# How to calculate two counts from 2 separate tables?

**URL:** <https://discuss.elastic.co/t/how-to-calculate-two-counts-from-2-separate-tables/183382>\
**Category:** Kibana\
**Created:** [May 29, 2019, 4:06pm UTC](https://discuss.elastic.co/t/how-to-calculate-two-counts-from-2-separate-tables/183382 "2019-05-29T16:06:29Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Havana](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/havana/32/47091_2.png) [@Havana](https://discuss.elastic.co/u/Havana)\
**Post date:** [May 29, 2019, 4:06pm UTC](https://discuss.elastic.co/t/how-to-calculate-two-counts-from-2-separate-tables/183382/1 "2019-05-29T16:06:29Z")

</div>

![28](https://us1.discourse-cdn.com/elastic/original/3X/7/a/7abf2069c2cff557deb6f052a8f346f0da432097.png)  
The left number represents the counts of total ad impressions from table A.  
The right number represents the counts of total ad clicks from table B.  
I want to create a new field named CTR(the left number / the right number)  
and visualize it on the dashboard.  
How can I do it?

---

<div class="post-metadata">

**Author:** ![Alona\_Nadler](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/alona_nadler/32/33499_2.png) [@Alona\_Nadler](https://discuss.elastic.co/u/Alona_Nadler)\
**Post date:** [May 29, 2019, 6:30pm UTC](https://discuss.elastic.co/t/how-to-calculate-two-counts-from-2-separate-tables/183382/2 "2019-05-29T18:30:44Z")

</div>

Hey  
You can do that using bucket script aggregation that is available in Visual Builder,  
bucket script allows you to create your own customized formula and it can use existing aggregation  
this is an example:

 ![image](https://us1.discourse-cdn.com/elastic/original/3X/f/d/fd36612981de0896d9a7567fcca1702b9fb3a2f9.png)  
I had 2 aggregations, sum(quantity) and sum(review) and then using bucket script I divided them

Let me know if you have any questions  
Alona

---

<div class="post-metadata">

**Author:** ![Havana](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/havana/32/47091_2.png) [@Havana](https://discuss.elastic.co/u/Havana)\
**Post date:** [May 30, 2019, 2:12am UTC](https://discuss.elastic.co/t/how-to-calculate-two-counts-from-2-separate-tables/183382/3 "2019-05-30T02:12:05Z")

</div>

Thanks Alona.  
I've come to this page which is in Visual Builder.  
I want to count total impressions from table A and total clicks from table B.  
So I should have 2 aggregations, one is count(created) from table A and another is count(created) from table B.  
How to choose different tables in this interface?  
Then I want to calculate click-through rate ( total\_clicks/total\_impressions)?

 ![50](https://us1.discourse-cdn.com/elastic/original/3X/7/7/7759d9b29742d89e93c6bf01a73f746398c41693.png)

---

<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:** [June 27, 2019, 3:44am UTC](https://discuss.elastic.co/t/how-to-calculate-two-counts-from-2-separate-tables/183382/5 "2019-06-27T03:44:25Z")

</div>

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