如何在SQL Server的where子句中使用别名列名

时间:2013-04-23 05:53:17

标签: sql-server-2005

当我尝试在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时,我收到错误。

我该如何解决这个问题?

5 个答案:

答案 0 :(得分:38)

您不能在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

答案 1 :(得分:27)

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

(*) - 好吧,只要结果“好像”在某个logical order中处理了SQL语句,系统就可以自由地重新排序操作。当然,SQL Server的所有问题都出现在produces errors的地方,因为SELECT子句中的行/值的转换问题应由WHERE子句消除。

答案 2 :(得分:1)

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子句。)

问题是您只能在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

答案 3 :(得分:0)

我认为您只能使用AS来显示最终值。要进行比较,select应该从其中的另一个select语句返回。

像:

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

你可以试试这个。

答案 4 :(得分:-2)

select 
    t1.*,
    t2.brand_name,
    t3.category_name,
    if(
        t3.parent_category_id=0,
        t3.cat_key,
        (
            select 
              concat(t3b.cat_key,'/',t3.cat_key) 
            from master_category t3b 
            where t3b.category_id=t3.parent_category_id 
        )
    ) as cat_key,
    (
        select min(t4.product_MSP) 
        from trans_products t4  
        where t1.product_id=t4.product_id 
    ) as product_MSP,
    (
        select min(t4.product_MRP) 
        from trans_products t4 
        where t1.product_id=t4.product_id 
    ) as product_MRP 
from master_products t1,
master_brand t2,
master_category t3 
where t1.status=1 
and t2.status=1 
and t2.brand_id=t1.brand_id 
and t3.category_id=t1.category_id 
and t3.display_status=1 
and t1.category_id=6 
and product_MSP between '1000' and '500000'