I had a look at various answers here and did a few experiments.
Specifically I am using MariaDB 10.1.
For a "simple" thing you can do what Robert D suggested in his comment:
SELECT Price_Per_SqFt, (Price_Per_SqFt/2) AS col1, (SELECT col1 + 1) AS col2 FROM Items
If you are using some sort of aggregate function with an inner join you can't use this, but you can combine this approach with the inner join approach as follows (NB VAT = "sales tax"... and NB in financial data currency fields typically have 4 decimal places, I think it's historic...)
SELECT
invoices.invoiceNo, invoices.clientID, invoices.Date, invoices.Paid,
invoicesWithSubtotal.Subtotal,
ROUND( CAST( Subtotal * invoices.VATRate AS DECIMAL( 10, 4 )), 2 ) AS VAT,
(SELECT VAT + Subtotal) AS Total
FROM invoices
INNER JOIN
( SELECT Sum( invoiceitems.Charge ) AS Subtotal, invoices.InvoiceNo FROM invoices
INNER JOIN invoiceitems ON invoices.InvoiceNo = invoiceitems.InvoiceNo
GROUP BY invoices.InvoiceNo ) invoicesWithSubtotal
ON invoices.InvoiceNo = invoicesWithSubtotal.InvoiceNo
I wanted to use the above to create a View
to list invoices with their subtotals, VAT and totals... it turned out that MariaDB (and almost certainly MySQL) don't allow nesting in the FROM
clause. However this is easily solved by making a first View
which lists the InvoiceNo
and Subtotal
, and then making a second View
which references the first. Performance-wise I have no idea at all about this sort of double-View
arrangement.