我有一个表,该表具有5个定义的列(id,colA,colB,colC,colD),所有数据类型均为decimal(18, 3)
,但id
除外。
SELECT
[id],
[colA], [colB], [colC], [colD],
colA - colB as length,
colC - colD * 2 as width,
//colA + width - length as colMod0
//modified the code
(select
colA + width - length
from [dbo].[main]
) as colMod0
//Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.
FROM
[dbo].[main]
ORDER BY
_main.id;
执行此代码时出现错误
将数据类型varchar转换为数字时出错
我试图进行转换和转换,但没有成功。长度,宽度和colMod0均为别名列。
该错误消息与类型无关。相反,它只是告诉您别名不能在同一级别的select语句中重用。一种解决方案是只重复width
和length
的表达式:
SELECT [id], [colA], [colB], [colC], [colD],
colA-colB AS length,
colC-colD*2 AS width,
colA + (colC-colD*2) - (colA-colB) AS colMod0
FROM [dbo].[main]
ORDER BY _main.id;