# Using SUM and LATEST in ESQL

**URL:** <https://discuss.elastic.co/t/using-sum-and-latest-in-esql/356501>\
**Category:** Kibana\
**Created:** [March 31, 2024, 1:07pm UTC](https://discuss.elastic.co/t/using-sum-and-latest-in-esql/356501 "2024-03-31T13:07:08Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![lizozom](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/lizozom/32/114932_2.png) [@lizozom](https://discuss.elastic.co/u/lizozom)\
**Post date:** [March 31, 2024, 1:07pm UTC](https://discuss.elastic.co/t/using-sum-and-latest-in-esql/356501/1 "2024-03-31T13:07:08Z")

</div>

I have log data that contains the name and price of an item purchased.  
For example:

```auto
.fruit-cost

Date, Name, Price
2024-03-01 12:00, Banana, 1.05
2024-03-01 11:00, Banana, 1.07
2024-03-01 11:30, Strawberry, 0.97

```

I would like to use `ES|QL` to calculate both the latest and total cost spend on each item.

```auto
FROM fruit-cost
| STATS total_cost = SUM(price) BY name

```

Gives me the total cost, but how to I fetch the latest price of each item?

Thanks,  
Liza

---

<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:** [April 5, 2024, 12:15pm UTC](https://discuss.elastic.co/t/using-sum-and-latest-in-esql/356501/2 "2024-04-05T12:15:35Z")

</div>

Hi @lizozom

I think this more an Elasticsearch question, but AFAIK that is not possible yet to do in ES|QL.  
I think this issue is sounds similar of your request: [ESQL: Aggregations feature requests · Issue #99999 · elastic/elasticsearch · GitHub](https://github.com/elastic/elasticsearch/issues/99999)

---

<div class="post-metadata">

**Author:** ![lizozom](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/lizozom/32/114932_2.png) [@lizozom](https://discuss.elastic.co/u/lizozom)\
**Post date:** [April 7, 2024, 7:35am UTC](https://discuss.elastic.co/t/using-sum-and-latest-in-esql/356501/3 "2024-04-07T07:35:54Z")

</div>

Thanks @Marco_Liberati .  
This is exactly what I'm after. Curious to see when this ends up on the roadmap, because currently the only alternative is to run a Transform and then ESQL on top of it.

---

<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:** [May 5, 2024, 7:36am UTC](https://discuss.elastic.co/t/using-sum-and-latest-in-esql/356501/4 "2024-05-05T07:36:19Z")

</div>

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