The examples here show a very simplified version of a VIEW
that I am constructing in MySQL. The goal here is to create a view from which I can select a single set of results to be populated into a HTML table.
The purpose of me using a view is, in part, to show calculations over this data as columns in the view. I can do this easily by giving aliases to expressions, such as: SELECT a, b, a + b AS c
will give me a resultset with three columns, of which the values in column c
are the addition of values in columns a
and b
Example 1
This is how I am currently constructing a VIEW over my fictional "product stock" table:
SELECT
description,
unit_cost,
current_stock,
required_stock,
(required_stock - current_stock) AS stock_shortfall,
(required_stock - current_stock) * unit_cost AS stock_shortfall_cost
FROM product_stock
It is important to me to be able to show both the stock shortfall, and the cost of replacing that stock in the table. You can see that this isn't fully optimised, as part of the stock shortfall calculation is used again to calculate the cost.
This also means that if something were to change in how the calculation of the stock shortfall was made, the same change would need to be made to the stock shortfall cost calculation, increasing the difficulty of maintaining the code.
Example 2 (Desirable Solution)
I can create a SELECT statement like this which will allow me to reference past expressions using user variables:
SELECT
description,
unit_cost,
current_stock,
required_stock,
@stock_shortfall := (required_stock - current_stock) AS stock_shortfall,
@stock_shortfall * unit_cost AS stock_shortfall_cost
FROM product_stock
I can also achieve a similar effect by using sub-queries on derived tables by referring to the alias of the expression in further calculations.
Unfortunately, MySQL will not allow me to use user variables or derived table sub-queries within a VIEW
.
Another solution could be to create a view in place of a derived table, and for each reused expression, create another view "layer" to select the derived value from. This works, but after so many layers, it also becomes unmaintainable.
I am currently performing a mixture of "layered" views and duplicate calculations like in Example #1. Please understand that the actual VIEW
I wish to construct is a JOIN
across 8 tables, with a lot of calculated values being reused in further expressions throughout. It is not very manageable.
Thanks for reading my wall of text. Here are my questions:
Is it possible to create a VIEW
using derived expressions in a similar way to example #2?
If not, how would you solve this problem?
What is the optimum solution? Should I be using a server-side script to calculate the values instead of using the database? Is there another database technology which does support this feature? What would you do?