[英]SQL select statement, calculating columns based on other columns
我试图总结单个价格并使用逻辑折扣。
SELECT O.OrderID,
if(C.IsClubMember & OrderNumber % 10 = 0, 0.5 * Sum(I.ItemPrice), Sum(I.ItemPrice)) as Price,
0.07*Price as Tax, Price + Tax as Total
FROM Orders as O JOIN ItemPriceView as I
ON O.OrderID = I.OrderID JOIN Customer as C
ON O.CustomerID = C.CustomerID
GROUP BY OrderID
我收到一个错误:
Error Code: 1054. Unknown column 'Price' in 'field list'
编写查询的正确方法是什么?
一种解决方案是使用子查询来计算价格,然后在外部查询中计算税款和总计。
这应该工作:
SELECT OrderID, Price, 0.07*Price as Tax, 1.07*Price as Total
FROM
(
SELECT O.OrderID as OrderID,
if(C.IsClubMember & OrderNumber % 10 = 0, 0.5 * Sum(I.ItemPrice),Sum(I.ItemPrice)) as Price
FROM Orders as O JOIN ItemPriceView as I
ON O.OrderID = I.OrderID JOIN Customer as C
ON O.CustomerID = C.CustomerID
GROUP BY OrderID
) as sub_query
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.