How do I sum all values from a single column of a table?

I am new to appsmith and I think this is pretty basic, however, I am trying to sum all of the values of a single column of a table and place it in a text widget (or something similar).

I have the database working smoothly and I can get the data into a table widget. I can update the values in the table and update the database. I know how to generally sum numbers in an array using javascript, however, I am not sure how to implement it most efficiently into appsmith. I have been through most of the help documents and cannot seem to find an answer. Any help would be appreciated.

Hi @Brent :wave:
You can do this either by summing over the data in javascript as
{{_.sumBy(Table1.tableData, (row) => row.value)}}
or if your data is very large, by returning an aggregate query and binding that to the text widget
select count(value) from table

Thanks @Nikhil.
In my table of 5 rows, the result I get is:
[object Object][object Object][object Object][object Object][object Object]
In the javascript you sent, how to do I specify the column to be summed?
Also, how best to specify that the column is numbers?

@Brent my mistake, the function is _.sumBy instead of _.sum
This is the lodash documentation for it. You specify the column as the return value in the second argument. In my example, value is the name of the column (row) => row.value
https://lodash.com/docs/4.17.15#sumBy

2 Likes

Thank you , it made my day

@Nikhil Thanks! That helped get me most of the way there. However, is now just concatenating the numbers. How best to specify the data type to be numbers not strings?
By the way, I am using google sheets as the data source, so this could be part of the issue. I have specified the table column as numbers in appsmith and it is also in google sheets, but no luck.

Nevermind. I got it - unless there is a more elegant way. Here is what I did:
{{_.sumBy(Table1.tableData, (row) => Number(row.value))}}

1 Like