# How can I add a sum() at the end of a table find() in cakephp 4.3

**URL:** <https://discourse.cakephp.org/t/how-can-i-add-a-sum-at-the-end-of-a-table-find-in-cakephp-4-3/10162>\
**Category:** Need Help\
**Created:** [February 12, 2022, 10:39pm UTC](https://discourse.cakephp.org/t/how-can-i-add-a-sum-at-the-end-of-a-table-find-in-cakephp-4-3/10162 "2022-02-12T22:39:42Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![mdeanquin](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/mdeanquin/32/1802_2.png) [@mdeanquin](https://discourse.cakephp.org/u/mdeanquin)\
**Post date:** [February 12, 2022, 10:39pm UTC](https://discourse.cakephp.org/t/how-can-i-add-a-sum-at-the-end-of-a-table-find-in-cakephp-4-3/10162/1 "2022-02-12T22:39:42Z")

</div>

I have a table of contributions and want to add a total to the table, how can I do that?

---

<div class="post-metadata">

**Author:** ![Zuluru](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/zuluru/32/1230_2.png) [@Zuluru](https://discourse.cakephp.org/u/Zuluru)\
**Post date:** [February 12, 2022, 11:25pm UTC](https://discourse.cakephp.org/t/how-can-i-add-a-sum-at-the-end-of-a-table-find-in-cakephp-4-3/10162/2 "2022-02-12T23:25:38Z")

</div>

You want to find just the sum of contributions that match your query? Or you want to get all the contribution records but also the sum of them?

---

<div class="post-metadata">

**Author:** ![mdeanquin](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/mdeanquin/32/1802_2.png) [@mdeanquin](https://discourse.cakephp.org/u/mdeanquin)\
**Post date:** [February 12, 2022, 11:45pm UTC](https://discourse.cakephp.org/t/how-can-i-add-a-sum-at-the-end-of-a-table-find-in-cakephp-4-3/10162/3 "2022-02-12T23:45:20Z")

</div>

I want to get all the records and the sum()

---

<div class="post-metadata">

**Author:** ![Zuluru](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/zuluru/32/1230_2.png) [@Zuluru](https://discourse.cakephp.org/u/Zuluru)\
**Post date:** [February 13, 2022, 12:03am UTC](https://discourse.cakephp.org/t/how-can-i-add-a-sum-at-the-end-of-a-table-find-in-cakephp-4-3/10162/4 "2022-02-13T00:03:25Z")

</div>

If you’re going to do something like iterate over all the records and output details from them, then building the sum as you go would be pretty straight-forward, and how I normally would handle that.

If it’s some other scenario where that doesn’t make sense, I don’t think you’ll be able to get both results from a single query. But depending on how your code is set up, you might be able to do the processing on the original query and then add a sum clause to it and re-execute to get that. If you were to share your query, people could probably help with what needs to be added to get the sum.

---

<div class="post-metadata">

**Author:** ![raul338](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/raul338/32/69_2.png) [@raul338](https://discourse.cakephp.org/u/raul338)\
**Post date:** [February 13, 2022, 3:36pm UTC](https://discourse.cakephp.org/t/how-can-i-add-a-sum-at-the-end-of-a-table-find-in-cakephp-4-3/10162/5 "2022-02-13T15:36:41Z")

</div>

You have to use [SQL functions](https://book.cakephp.org/4/en/orm/query-builder.html#using-sql-functions).  
But if you want the results AND the sum of all then you have to do two queries.

```auto
$results = $this->Contributions->find()->all()->toArray(); // or paginate or something
$query = $this->Contributions->find()->func()->sum('amount');
$total = $query->first()->toArray();

```
