2013-04-16 34 views
0

我在想,如果你可以參考的東西where子句,如:參考的東西在SELECT子句SQL

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

非常感謝

ķ

+0

如果您的意思是WHERE子句可以訪問結果x,y和z,那麼答案是否定的。儘管它們可以在'HAVING'子句中訪問,它在返回之前在查詢結果上運行。因此它效率較低,因此只有在沒有更好的解決方案時才能使用。 – Michael

回答

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 
+0

謝謝。這是一種恥辱...我希望我可以做到這一點懶惰的方式 –

+0

赫赫只是選擇最適合你的一個':D' –

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在那裏,支持你的語法,雖然。

+0

沒有支持':)' –