我有一个如下所示的查询:
Select (numberLateOrders / numberOfOrders) as percentFailed
From Orders
现在我希望能够添加Where
子句并访问列别名percentFailed
,但是在查询完成之前似乎无法识别别名。这是真的和/或有解决方法吗?
谢谢!
答案 0 :(得分:3)
是的,列WHERE
子句中的列别名将无法访问/可用,因为SQL中执行的逻辑顺序是SELECT column alias
之后的WHERE
所以WHERE
将无法识别它们。您必须再次在WHERE
中添加计算/表达式,如下所示
Select (numberLateOrders / numberOfOrders) as percentFailed
From Orders
WHERE (numberLateOrders / numberOfOrders) > some_value
另一个解决方案(不是更好的解决方案)是使用外部查询,如
select percentFailed
from
(
Select (numberLateOrders / numberOfOrders) as percentFailed
From Orders
) tab
where percentFailed > some_value
答案 1 :(得分:2)
只是添加到现有答案
或者使用子查询
select
percentFailed
from (
Select (numberLateOrders / numberOfOrders) as percentFailed
From Orders
) t
where
percentFailed > 0
答案 2 :(得分:1)
在哪里看到别名,只需重复表达
Select (numberLateOrders / numberOfOrders) as percentFailed
From Orders
WHERE (numberLateOrders / numberOfOrders) >0