0

我想知道您是否可以在 where 子句中引用某些内容,例如:

select
sum([some calculation]) as x,
sum([some other calculation]) as y,
x/y as z
from 
[rest of the sql...]

非常感谢

ķ

4

2 回答 2

1

不,您不能使用在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
于 2013-04-16T12:27:11.607 回答
1

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 支持您的语法。

于 2013-04-16T12:28:40.260 回答