我想知道您是否可以在 where 子句中引用某些内容,例如:
select
sum([some calculation]) as x,
sum([some other calculation]) as y,
x/y as z
from
[rest of the sql...]
非常感谢
ķ
不,您不能使用在SELECT
语句的同一级别上生成的别名。
以下是可能的实现方式。
使用原始公式:
select sum([some calculation]) as x,
sum([some other calculation]) as y,
sum([some calculation]) / sum([some other calculation]) as z
from tableName
或通过使用子查询:
SELECT x,
y,
x/y z
FROM
(
select sum([some calculation]) as x,
sum([some other calculation]) as y
from tableName
) s
SQL 标准不支持这一点。你必须写:
select
sum([some calculation]) as x,
sum([some other calculation]) as y,
sum([some calculation])/sum([some other calculation]) as z
from
[rest of the sql...]
不过,可能有一些 RDBMS 支持您的语法。