如何使用别名列名其中,在SQL Server条款(How to use alias column n

2019-08-31 21:11发布

当我试图执行SQL Server中下面的代码,2005年,我正在错误

无效的列名DistanceFromAddress

码:

select 
    SQRT(POWER(cast(Program_Latitude as float) - cast('41.5126237' as float), 2) +   
    POWER(cast(Program_Longitude as float) - cast('-81.6516411' as float), 2)) * 62.1371192 AS DistanceFromAddress 
from 
    tblProgram 
where 
    DistanceFromAddress < 2

我得到的值正确使用SELECT语句,但是当我试图检查的条件where DistanceFromAddress < 2我得到的错误。

我怎样才能解决这个问题呢?

Answer 1:

你不能在使用别名列WHERE子句。 您可以尝试使用派生表。 也许这样的事情(对不起,未测试):

SELECT * FROM
(SELECT SQRT(POWER(cast(Program_Latitude as float) - cast('41.5126237' as float), 2) +   
 POWER(cast(Program_Longitude as float) - cast('-81.6516411' as float), 2)) * 62.1371192 
 AS DistanceFromAddress from tblProgram) mytable
WHERE DistanceFromAddress < 2


Answer 2:

WHERE子句的处理SELECT从句(*),因此别名不可用。 移动到使用子查询或CTE -这里有一个CTE:

; with Distances as (
    select SQRT(POWER(cast(Program_Latitude as float) - cast('41.5126237' as float), 2) +   
 POWER(cast(Program_Longitude as float) - cast('-81.6516411' as float), 2)) * 62.1371192 
 AS DistanceFromAddress
    from tblProgram
)
select * from Distances where DistanceFromAddress < 2

(*) -阱,系统是免费的,因为他们认为合适的重新排序操作,只要结果是“好像” SQL语句是在一定的处理逻辑顺序 。 当然,在这一切都错了与SQL Server是它产生错误因为在转换问题SELECT的行/值条款,应当由被淘汰WHERE子句。



Answer 3:

select 
  SQRT(
    POWER(cast(Program_Latitude as float) - cast('41.5126237' as float), 2) +
    POWER(cast(Program_Longitude as float) - cast('-81.6516411' as float), 2)
  ) * 62.1371192 AS DistanceFromAddress 
from tblProgram 
having DistanceFromAddress < 2

可以工作(虽然我认为不会,而无需GROUP BY子句为好)。

问题是,你只能使用名称表中(S)的范围,你里面选择where的条款。 Where是预过滤器,过滤出选择行之前他们,所以像这样的表达式中的字段定义尚未执行,并且别名为此不可用。

Having条款可以作为一个后置滤波器分组后,可以使用别名从查询,但我怕你将需要有一个实际的group by子句(不知道)。

另一种方法是为具有子选择(派生的表或在所选择的选择),其中,首先选择每一行的距离,然后选择只能从这些结果的相关距离。 这将工作:

select d.DistanceFromAddress
from
  (select 
    SQRT(
      POWER(cast(Program_Latitude as float) - cast('41.5126237' as float), 2) +
      POWER(cast(Program_Longitude as float) - cast('-81.6516411' as float), 2)
    ) * 62.1371192 AS DistanceFromAddress 
  from tblProgram) d
where d.DistanceFromAddress < 2

或者你可以重复表达。 这使得您的查询更难维护,但在某些情况下,这可能会为你工作。 例如,如果你不会想返回的实际距离,但只,说,利益在这个距离点的名称。 在这种情况下,你需要有唯一的表达where条款,在这种情况下,维护说法走了,这解决方案是一个完美的选择。

select 
  tblProgram.POIname
  /* Only if you need to return the actual value
  , SQRT(
    POWER(cast(Program_Latitude as float) - cast('41.5126237' as float), 2) +
    POWER(cast(Program_Longitude as float) - cast('-81.6516411' as float), 2)
  ) * 62.1371192 AS DistanceFromAddress */
from tblProgram
where 
  -- Use this if you only want to filter by the value.
  SQRT(
    POWER(cast(Program_Latitude as float) - cast('41.5126237' as float), 2) +
    POWER(cast(Program_Longitude as float) - cast('-81.6516411' as float), 2)
  ) * 62.1371192 < 2


Answer 4:

我想你只能用作显示最终值。 为了做一个比较,选择应当从里面另一个选择语句返回。

喜欢:

SELECT a.disfromaddr FROM
 ( SELECT SQRT(POWER(cast(Program_Latitude as float) - cast('41.5126237' as float), 2) +   
 POWER(cast(Program_Longitude as float) - cast('-81.6516411' as float), 2)) * 62.1371192 
 AS DistanceFromAddress FROM tblProgram) 
a WHERE a.disfromaddr < 2

你可以试试这个。



文章来源: How to use alias column name in where clause in SQL Server