# Query to get all records until the sum of column less than or equal a value

**URL:** <https://discuss.elastic.co/t/query-to-get-all-records-until-the-sum-of-column-less-than-or-equal-a-value/197302>\
**Category:** Elasticsearch\
**Created:** [August 29, 2019, 9:33am UTC](https://discuss.elastic.co/t/query-to-get-all-records-until-the-sum-of-column-less-than-or-equal-a-value/197302 "2019-08-29T09:33:42Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![darshitdrg](https://avatars.discourse-cdn.com/v4/letter/d/f07891/32.png) [@darshitdrg](https://discuss.elastic.co/u/darshitdrg)\
**Post date:** [August 29, 2019, 9:33am UTC](https://discuss.elastic.co/t/query-to-get-all-records-until-the-sum-of-column-less-than-or-equal-a-value/197302/1 "2019-08-29T09:33:42Z")

</div>

HI,

Is there any way to find ids of document for which sum of field "salary" reach 10000?  
I just want to fetch ids for which the total of salary is just less than or equal to 10000?

something like - [https://stackoverflow.com/questions/21824383/query-to-get-all-records-until-the-sum-of-column-less-than-or-equal-a-value](https://stackoverflow.com/questions/21824383/query-to-get-all-records-until-the-sum-of-column-less-than-or-equal-a-value)

Thank you in advance.

---

<div class="post-metadata">

**Author:** ![darshitdrg](https://avatars.discourse-cdn.com/v4/letter/d/f07891/32.png) [@darshitdrg](https://discuss.elastic.co/u/darshitdrg)\
**Post date:** [August 30, 2019, 6:01am UTC](https://discuss.elastic.co/t/query-to-get-all-records-until-the-sum-of-column-less-than-or-equal-a-value/197302/2 "2019-08-30T06:01:22Z")

</div>

Can anyone get a time to look into this problem?

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [August 30, 2019, 7:04am UTC](https://discuss.elastic.co/t/query-to-get-all-records-until-the-sum-of-column-less-than-or-equal-a-value/197302/3 "2019-08-30T07:04:52Z")

</div>

Read [this](https://discuss.elastic.co/t/about-the-elasticsearch-category/21) and specifically the "Also be patient" part.

It's fine to answer on your own thread after 2 or 3 days (not including weekends) if you don't have an answer.

---

<div class="post-metadata">

**Author:** ![darshitdrg](https://avatars.discourse-cdn.com/v4/letter/d/f07891/32.png) [@darshitdrg](https://discuss.elastic.co/u/darshitdrg)\
**Post date:** [August 30, 2019, 7:14am UTC](https://discuss.elastic.co/t/query-to-get-all-records-until-the-sum-of-column-less-than-or-equal-a-value/197302/4 "2019-08-30T07:14:45Z")

</div>

@dadoonet I understood. Thanks.

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [September 3, 2019, 3:41am UTC](https://discuss.elastic.co/t/query-to-get-all-records-until-the-sum-of-column-less-than-or-equal-a-value/197302/5 "2019-09-03T03:41:40Z")

</div>

Could you provide a full recreation script as described in [About the Elasticsearch category](https://discuss.elastic.co/t/about-the-elasticsearch-category/21). It will help to better understand what you are doing. Please, try to keep the example as simple as possible.

A full reproduction script will help readers to understand, reproduce and if needed fix your problem. It will also most likely help to get a faster answer.

So provide an example dataset we can play from.

---

<div class="post-metadata">

**Author:** ![darshitdrg](https://avatars.discourse-cdn.com/v4/letter/d/f07891/32.png) [@darshitdrg](https://discuss.elastic.co/u/darshitdrg)\
**Post date:** [September 9, 2019, 8:11am UTC](https://discuss.elastic.co/t/query-to-get-all-records-until-the-sum-of-column-less-than-or-equal-a-value/197302/6 "2019-09-09T08:11:50Z")

</div>

I want to get all the records in a table until the sum of a particular column is less than or equal to 'n'.

For SQL example:

Table: data

slno item price

1 item1 1000  
2 item2 2000  
3 item3 3000  
4 item4 4000  
5 item5 5000  
6 item6 6000

I want to get all records from above table until the sum(price) is less than or equal to 10000

So, with the above table my result would be first 4 records.

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [September 9, 2019, 8:24am UTC](https://discuss.elastic.co/t/query-to-get-all-records-until-the-sum-of-column-less-than-or-equal-a-value/197302/7 "2019-09-09T08:24:42Z")

</div>

As they are different documents, I don't see how you can do that.  
I believe you need to do it on the client side, do the sum manually and if you don't have enough records, ask for a new page of records...

No idea on my end.

---

<div class="post-metadata">

**Author:** ![darshitdrg](https://avatars.discourse-cdn.com/v4/letter/d/f07891/32.png) [@darshitdrg](https://discuss.elastic.co/u/darshitdrg)\
**Post date:** [September 9, 2019, 9:08am UTC](https://discuss.elastic.co/t/query-to-get-all-records-until-the-sum-of-column-less-than-or-equal-a-value/197302/8 "2019-09-09T09:08:18Z")

</div>

Ok.  
Thank you for your time !!!

---

<div class="post-metadata">

**Author:** ![darshitdrg](https://avatars.discourse-cdn.com/v4/letter/d/f07891/32.png) [@darshitdrg](https://discuss.elastic.co/u/darshitdrg)\
**Post date:** [September 10, 2019, 10:38am UTC](https://discuss.elastic.co/t/query-to-get-all-records-until-the-sum-of-column-less-than-or-equal-a-value/197302/9 "2019-09-10T10:38:53Z")

</div>

same thing is possible in SQL. So, i think ElasticSearch should support.

---

<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:** [October 8, 2019, 10:38am UTC](https://discuss.elastic.co/t/query-to-get-all-records-until-the-sum-of-column-less-than-or-equal-a-value/197302/10 "2019-10-08T10:38:55Z")

</div>

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