我正在尝试计算项目的总成本,并将该值放入名为cost的新列中。
在我的创建脚本中,值以字符为单位,但数据类型为money。
CREATE VIEW ordered_pizzas_view
AS
SELECT
orderpizza_id, order_id, ...,
CONCAT(range_price, ' ', crust_surcharge, ' ', sauce_surcharge) AS 'cost'
FROM
order_pizza
INNER JOIN
...
WHERE
'cost' = (SELECT SUM(CAST(range_price AS MONEY)) FROM pizza_range) +
(SELECT SUM(CAST(crust_surcharge AS MONEY)) FROM crust) +
(SELECT SUM(CAST(Sauce_surcharge AS MONEY)) FROM sauce)
答案 0 :(得分:3)
您对concat()
的期望是什么? concat()
返回一个字符串值,该字符串值基于您的输入将是由空格分隔的三个货币值,例如$1.00 $2.00 $0.50
将其与金钱价值进行比较会产生错误。也许你只想添加值?
(range_price + crust_surcharge + sauce_surcharge ) as [cost]
注意:不要将字符串文字用作别名。
此外,您的where
子句正在将字符串文字('cost'
)与组合子查询中的money
数据类型进行比较。
where --'cost'
(range_price + crust_surcharge + sauce_surcharge )
= (select Sum(CAST(range_price as money)) from pizza_range)
+ ( select Sum(CAST(crust_surcharge as money)) from crust )
+ ( select Sum(CAST(Sauce_surcharge as money)) from sauce )
注意:您不能在where
子句中使用列别名,您必须重复表达式或引用子查询/派生表或其他运算符中的表达式。