Best way to do a simple-ish calculation (sum two fields in a table, convert into a percentage)

Hey all,

I have two fields in a DB table, I want to sum those, then I want to divide one by the other and * by 100 to get the percentage.

I know you can do basic maths with the operator, but I can’t seem to get the above working where I first need to sum the field, and divide by the other sum’d field.

Any ideas how I can do this within Wappler?

In the standard query building you can use the aggregate function SUM, just make sure you give it an alias (no spaces).

You could then do another calculation in app connect between the two fields to display it as a %

1 Like

thank you, will try this!

You could to it all In a custom query as well, but it just depends on how your displaying the end result and at what level (I.e. daily, monthly etc).

It’s for some basic analytics, so it’s real-time - would you recommend that this is then taken care of with the sum aggregator and alias?

You should be able to achieve the basic calculations in a normal query. Then any aggregate fields you get in the query you can perform further calculations in App Connect.

i.e. in the query you could have

COUNT of orders AS orders
SUM of order value AS value

and in app connect you could have value / orders to get and average amount.

You just need to be mindful of the way it groups the data (which you can see in the SQL query it writes as you add the aggregates in).

You want to make sure its group on the right ‘dimension’ that you want. For me my data is all based on either days or time intervals so that’s how i see it.

1 Like