# How to query a sum and it's percentage in the same query?

**URL:** <https://discuss.elastic.co/t/how-to-query-a-sum-and-its-percentage-in-the-same-query/381229>\
**Category:** Elasticsearch\
**Tags:** esql\
**Created:** [August 22, 2025, 8:50am UTC](https://discuss.elastic.co/t/how-to-query-a-sum-and-its-percentage-in-the-same-query/381229 "2025-08-22T08:50:08Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![khat33b](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/khat33b/32/16644_2.png) [@khat33b](https://discuss.elastic.co/u/khat33b)\
**Post date:** [August 22, 2025, 8:50am UTC](https://discuss.elastic.co/t/how-to-query-a-sum-and-its-percentage-in-the-same-query/381229/1 "2025-08-22T08:50:08Z")

</div>

I am writing an ES|QL to find out the sum and of a field grouped by another field and also it’s percentage by other sums of that field. How do I write the query?

**`FROM`** ` test_index`  
`| ` **`WHERE`** ` start_time >= DATE_PARSE("yyyy-MM-dd'T'HH:mm:ssXXX", "2025-08-01T00:00:00Z") AND start_time <= DATE_PARSE("yyyy-MM-dd'T'HH:mm:ssXXX", "2025-08-20T00:00:00Z")`  
`| ` **`STATS`** ` views_total = SUM(1), view_time = SUM(view_time)` **`BY`** `device_type,user_category`  
`// here I also need to find the percent of view_time too`  
`| ` **`EVAL`** ` view_time_mins = ROUND(TO_DOUBLE(view_time) / 60.0, 2)`

Here I also need to find the percent of view\_time too but can’t get the total sum of view\_time without any groups in the `STATS` command.

---

<div class="post-metadata">

**Author:** ![Tortoise](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tortoise/32/147587_2.png) [@Tortoise](https://discuss.elastic.co/u/Tortoise)\
**Post date:** [August 22, 2025, 11:01am UTC](https://discuss.elastic.co/t/how-to-query-a-sum-and-its-percentage-in-the-same-query/381229/2 "2025-08-22T11:01:20Z")

</div>

Hello @khat33b

Please check below post regarding your question to show % , if it is helpful :

> [@Creating a Kinana table using a multi-field with es|ql](https://discuss.elastic.co/t/creating-a-kinana-table-using-a-multi-field-with-es-ql/377972):
>
> v 8.17.4 I am trying to create a visualisation table in Kibana using an es|ql query. The main field in question is a Keyword multi-field call ItemsFound. I want the first column to be a list of unique items found. Column two is a count of the ItemsFound Column three will be a percentage of ItemsFound I can get the first two columns working just using Kibana but I can't get the percentage, even with a formula. ItemsFound ["Item B", "Item W", "Item H"] ItemsFound [ "Item B", "Item R", "…

Thanks!!

---

<div class="post-metadata">

**Author:** ![khat33b](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/khat33b/32/16644_2.png) [@khat33b](https://discuss.elastic.co/u/khat33b)\
**Post date:** [August 22, 2025, 2:02pm UTC](https://discuss.elastic.co/t/how-to-query-a-sum-and-its-percentage-in-the-same-query/381229/3 "2025-08-22T14:02:59Z")

</div>

All that link says that it cannot be done.

---

<div class="post-metadata">

**Author:** ![Tortoise](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tortoise/32/147587_2.png) [@Tortoise](https://discuss.elastic.co/u/Tortoise)\
**Post date:** [August 22, 2025, 2:09pm UTC](https://discuss.elastic.co/t/how-to-query-a-sum-and-its-percentage-in-the-same-query/381229/4 "2025-08-22T14:09:32Z")

</div>

Hello,

For the % if you select the view as Pie chart it automatically calculates the % which can be used if you are ok.

Thanks!!

---

<div class="post-metadata">

**Author:** ![khat33b](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/khat33b/32/16644_2.png) [@khat33b](https://discuss.elastic.co/u/khat33b)\
**Post date:** [August 22, 2025, 2:49pm UTC](https://discuss.elastic.co/t/how-to-query-a-sum-and-its-percentage-in-the-same-query/381229/5 "2025-08-22T14:49:44Z")

</div>

Hi,

I am not using Kibana. I am using this query in my backend code and fetching the result.
