christopher's avatar

MySQL Database calculate total sum

I've a database table which contains several of items. Each item has a quantity and a column of cost.

Now i want to get all items which are related to the unique invoice_id get the total sum of each table row and then calculate the total cost. Is this possible with one single SQL statement? And is this the best way to calculate the total cost of an invoice?

So to calculate the total cost "on the fly" instead of storing the total sum to the database? Or should i store the total net cost of each invoice ( or maybe each invoice item? ) in the database?

I attached an image of my table structure -> https://imgur.com/a/TaGpmP9

0 likes
5 replies
Talinon's avatar

You could certainly build a query to handle this, but I strongly suggest against it. A few years ago, I had a couple projects very similar to this where I asked myself the same question. Originally, I developed it to calculate on-the-fly, by taking all the sum of all the related lines (quantity * price) - very similar to your situation. As the projects grew, more business logic was required, which added many layers of complexity. For an example, let's take a quotation. The original quote's body might sum up to an amount, but maybe doesn't take into account extra charges, overhead, extension of work, etc. Scenarios like this arose to the need to add a user-specified "total bid value", which if present, would override the lines calculation. You can see how conditionals such as this can easily break-down and cause headaches in trying to go down this road.

Instead, I suggest that at the time of persistence (including update) is when you perform your net total calculation, and then save it as a field on your parent record. This still gives you the ability to perform on-the-fly calculations, but without the overhead of performing costly joins and other additional calculations. I also have some experience with ERP integrations, and many enterprise solutions take this same approach of storing the calculated total.

christopher's avatar

@talinon Thanks for your detailed answer. So you would add a total column to the invoice_items table and save the total net value while saving the model?

And then get all total values of the invoice_items which are related to the invoice and calculate the actual total value on the fly with taxes etc. ?

esorone's avatar

Hello Christopher,

If it was up to me, I would indeed make a total column and store the (cost * quantity) value in a total column. If the table growth in records, it will save a tremendous amount of time and calculations. Now you only have to sum(Total) and other statistic possibilities as well. E.g. avg()

Talinon's avatar

I wouldn't calculate the total value by summing the related lines, even if you store the line extensions. It is still making unnecessary calculations that can significantly slow down your app. Just do all the calculations at the time of saving your model, and store the total on the invoices table. I don't know the scope of your project, but mine quickly escalated into a mess when reporting needed to be broken down by region, territory, sales manager, subsidiary-company, item class, etc. If you're already performing complex queries and filtering, you don't want to have to dive into related lines and do further calculations.

As I said, by having the total on the invoice model, you can still perform your on-the-fly operations, such as reporting, lists, etc, but you can just simply reference the total value - you'll thank yourself for this later on.

You bring up a good point about taxes. So, I would consider having these fields: total, tax, extension all on the model. The fact that there can be multiple types of tax is not lost on me - in situations where that kind of detail would be required is when I'd dive into the related data to gather it - but chances are, if you need that type of data, you're probably "viewing" the invoice and retrieving all the data anyway.

1 like
christopher's avatar

@TALINON - Thanks again for the detailed reply. I really appreciate it.

I followed your suggestion and i'm saving now the total net amount to the table when i'm saving the model with it's relationships.

Please or to participate in this conversation.