I need some advice.
Ok, I know it's very bad to store calculated values, but I'm writing an app to keep track of orders and invoices. I keep my taxes in a lookup table, and if they change, I don't want to see incorrect values for, say, last year's invoices when I look them up.
So I think I should store the totals for each invoice upon creation, by calculating them upon creation of the invoice. Currently, when I create an invoice, I run an update query that DSums the columns in another query (4 times) that calculates the subtotals, two taxes, and totals for each order that's going on an invoice, and then sticks those numbers into my Invoice table (so at any point in the future I can pull up that invoice, regardless of whether tax rates have changed since then.
This query takes forever.
Problem is, I've got Invoices, Orders, OrderItems, and Taxes. Each order has many items. Each item can have tax1 and/or tax2 - it stores a bool for each. If I want to calculate the value of an invoice, I need to know which orders have that invoice number, then which orderitems have those order numbers, then find out which taxes are applicable to each orderitem and look up their values in the tax table. Then I have to sum the subtotals, tax1, tax2, and totals for every item on every order that's on my invoice.
I have a feeling that although my design is partially correct, I'm doing something very wrong.
Is there a better way?
Ok, I know it's very bad to store calculated values, but I'm writing an app to keep track of orders and invoices. I keep my taxes in a lookup table, and if they change, I don't want to see incorrect values for, say, last year's invoices when I look them up.
So I think I should store the totals for each invoice upon creation, by calculating them upon creation of the invoice. Currently, when I create an invoice, I run an update query that DSums the columns in another query (4 times) that calculates the subtotals, two taxes, and totals for each order that's going on an invoice, and then sticks those numbers into my Invoice table (so at any point in the future I can pull up that invoice, regardless of whether tax rates have changed since then.
This query takes forever.
Problem is, I've got Invoices, Orders, OrderItems, and Taxes. Each order has many items. Each item can have tax1 and/or tax2 - it stores a bool for each. If I want to calculate the value of an invoice, I need to know which orders have that invoice number, then which orderitems have those order numbers, then find out which taxes are applicable to each orderitem and look up their values in the tax table. Then I have to sum the subtotals, tax1, tax2, and totals for every item on every order that's on my invoice.
I have a feeling that although my design is partially correct, I'm doing something very wrong.
Is there a better way?
Last edited: