# Sum 2 queries in Canvas element

**URL:** <https://discuss.elastic.co/t/sum-2-queries-in-canvas-element/272763>\
**Category:** Kibana\
**Tags:** canvas\
**Created:** [May 12, 2021, 5:41am UTC](https://discuss.elastic.co/t/sum-2-queries-in-canvas-element/272763 "2021-05-12T05:41:37Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![AClerk](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/aclerk/32/55297_2.png) [@AClerk](https://discuss.elastic.co/u/AClerk)\
**Post date:** [May 12, 2021, 5:41am UTC](https://discuss.elastic.co/t/sum-2-queries-in-canvas-element/272763/1 "2021-05-12T05:41:38Z")

</div>

Hello,  
I need to sum up 2 numbers from 2 different queries.  
I read [this post](https://discuss.elastic.co/t/sql-dividing-2-values-from-2-queries-in-canvas/222381) but was not able to achieve any result.  
Can someone please focus me on the solution?  
Need to achieve something like this:

```auto
select amount from my-index where bla=x 
+ 
select amount from my-index where bla=y

```

Cheers!

---

<div class="post-metadata">

**Author:** ![Marco\_Liberati](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/marco_liberati/32/82953_2.png) [@Marco\_Liberati](https://discuss.elastic.co/u/Marco_Liberati)\
**Post date:** [May 12, 2021, 9:42am UTC](https://discuss.elastic.co/t/sum-2-queries-in-canvas-element/272763/2 "2021-05-12T09:42:48Z")

</div>

Hi @AClerk ,

I've used the same expression in the post you referenced to and it works by changing the `/` divide operator with the `+` one. Then you have to change the queries with yours, and all the referenced fields:

```auto
filters group="order_date" group="gender" ungrouped=true
| essql 
    query="SELECT order_date, customer_gender FROM \"kibana_sample_data_ecommerce\""
    count=10000
| math { 
    string "count(customer_gender)+" // <- changed the operation from / to + here
      {filters group="order_date" ungrouped=true 
       | essql query="SELECT order_date, customer_gender FROM \"kibana_sample_data_ecommerce\""
           count=10000
       | math "count(customer_gender)"
      } 
  }

```

The idea is to build a string with the math formula, and then run the `math` operation over it

---

<div class="post-metadata">

**Author:** ![AClerk](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/aclerk/32/55297_2.png) [@AClerk](https://discuss.elastic.co/u/AClerk)\
**Post date:** [May 12, 2021, 10:38pm UTC](https://discuss.elastic.co/t/sum-2-queries-in-canvas-element/272763/3 "2021-05-12T22:38:10Z")

</div>

Will give it another try.  
Thanks for your reply.

---

<div class="post-metadata">

**Author:** ![Felix\_Roessel](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/felix_roessel/32/41623_2.png) [@Felix\_Roessel](https://discuss.elastic.co/u/Felix_Roessel)\
**Post date:** [May 13, 2021, 5:48am UTC](https://discuss.elastic.co/t/sum-2-queries-in-canvas-element/272763/4 "2021-05-13T05:48:26Z")

</div>

Personally I would prefer storing the results of the queries in variables first and do the calculation in a second step.  
This way the expression is easier to understand I think.

---

<div class="post-metadata">

**Author:** ![AClerk](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/aclerk/32/55297_2.png) [@AClerk](https://discuss.elastic.co/u/AClerk)\
**Post date:** [May 24, 2021, 1:23am UTC](https://discuss.elastic.co/t/sum-2-queries-in-canvas-element/272763/5 "2021-05-24T01:23:14Z")

</div>

@Felix_Roessel  
Do you have an example?  
Was looking for this option.  
Thanks

---

<div class="post-metadata">

**Author:** ![Felix\_Roessel](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/felix_roessel/32/41623_2.png) [@Felix\_Roessel](https://discuss.elastic.co/u/Felix_Roessel)\
**Post date:** [May 28, 2021, 8:04am UTC](https://discuss.elastic.co/t/sum-2-queries-in-canvas-element/272763/6 "2021-05-28T08:04:36Z")

</div>

It could look somehow like this...

```
filters
  | essql query={string "SELECT service.name FROM \"apm*\" where processor.event = 'error' "}
  | var_set name="field1" value={getCell "field1"}
| filters
  | essql query={string "SELECT service.name FROM \"apm*\" where processor.event = 'transaction' "}
  | var_set name="field2" value={getCell "field2"}
| math {string {var field1} " + " {var field2} }
```

---

<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 25, 2021, 8:05am UTC](https://discuss.elastic.co/t/sum-2-queries-in-canvas-element/272763/7 "2021-06-25T08:05:20Z")

</div>

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