I am trying to sum a column that has an alias name (since to create the column I had to do math on other columns). I know that you can't do this (aggregate functions on aliases created within the same select query) but none of the solutions I have searched seem to work for me.
This my code
Select
o.Order_Value Total_Price_Before_Discount,
od.Price*od.Quantity_Ordered as Line_Price_Before_Discount,
od.Discount_Value, od.Discount_Percentage,
od.Price*od.Quantity_Ordered-od.Discount_Value as Line_Price_After_Discount,
sum(Line_Price_After_Discount) as Total_Price_After_Discount, -- this is the problem line
from Orders o (nolock)
join Order_Detail od (nolock) on
o.Company_Code=od.Company_Code and o.Division_Code=od.Division_Code
I tried
sum(od.Price*od.Quantity_Ordered-od.Discount_Value) as Total_Price_After_Discount
but that didn't work and I get a group by error
invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
Which I don't fully understand.