I am new to SQL views, so be gentle!
I have the following kind of SQL:
SELECT dbo.product.name AS [Product Name], ROUND(CASE [vat] WHEN 1 THEN [packcost] * 1.2 ELSE [packcost] END, 2) AS [Pack Cost], ROUND(CASE [vat] WHEN 1 THEN ([packcost] * 1.2) / [units] ELSE [packcost] / [units] END, 2) AS [Unit Cost], dbo.purchase.unitsaleprice * dbo.product.units AS [ Pack Sale Price], dbo.purchase.unitsaleprice AS [Unit Sale Price], dbo.product.units * (dbo.purchase.unitsaleprice - ROUND(CASE [vat] WHEN 1 THEN ([packcost] * 1.2) / [units] ELSE [packcost] / [units] END, 2)) AS [Pack Profit], dbo.purchase.unitsaleprice - ROUND(CASE [vat] WHEN 1 THEN ([packcost] * 1.2) / [units] ELSE [packcost] / [units] END, 2) AS [Unit Profit] FROM dbo.product INNER JOIN dbo.purchase ON dbo.product.id = dbo.purchase.productID
But that seems ineffective as I rewrite a lot of it.
For example, I would like to define the [Pack Cost] column:
ROUND(CASE [vat] WHEN 1 THEN [packcost] * 1.2 ELSE [packcost] END, 2) AS [Pack Cost]
to use it instead of rewriting it.
for example, so I could use:
[Pack Cost] / [Units]
To determine unit cost instead:
ROUND(CASE [vat] WHEN 1 THEN ([packcost] * 1.2) / [units] ELSE [packcost] / [units] END, 2) AS [Unit Cost]
Not sure if I get the correct end of the stick, or is it advisable to do so.
source share