"列名无效" (在where子句中使用别名)

时间:2018-01-22 12:51:50

标签: sql sql-server sql-server-2008

我从SQL Server收到此错误消息:

  

列名称无效'价格'

在Microsoft SQL Server Management Studio中。

我的SQL声明:

SELECT
    CASE
       WHEN salePriceDate BETWEEN salePriceStartDate AND salePriceEndDate
          THEN salePrice
          ELSE 
             CASE
                WHEN salePriceDate2 BETWEEN salePriceStartDate2 AND salePriceEndDate2
                   THEN salePrice2
                   ELSE normalPrice
             END
    END AS price
FROM 
    prices
WHERE 
    price BETWEEN 1 AND 100

我该如何解决这个问题?

5 个答案:

答案 0 :(得分:5)

SQL不允许您使用SELECT子句(或大多数其他子句)中WHERE中定义的别名。这是SQL语言的一部分。

有多种方法可以解决这个问题。一种方法是子查询。另一个是CTE。 SQL Server还提供横向连接:

SELECT p.*, v.price
FROM prices p OUTER APPLY
     (VALUES (CASE WHEN p.salePriceDate BETWEEN p.salePriceStartDate AND p.salePriceEndDate
                   THEN p.salePrice
                   WHEN p.salePriceDate2 BETWEEN p.salePriceStartDate2 p.AND salePriceEndDate2
                   THEN p.salePrice2
                   ELSE p.normalPrice
              END)
     ) v(price)
WHERE v.price BETWEEN 1 AND 100;

另请注意,这简化了CASE表达式。无需嵌套CASE

答案 1 :(得分:1)

请尝试此嵌套查询。在这种情况下,计算字段将具有where子句,而不必两次执行case语句。

select * from
(
SELECT
    CASE
       WHEN salePriceDate BETWEEN salePriceStartDate AND salePriceEndDate
          THEN salePrice
          ELSE 
             CASE
                WHEN salePriceDate2 BETWEEN salePriceStartDate2 AND salePriceEndDate2
                   THEN salePrice2
                   ELSE normalPrice
             END
    END AS price
FROM 
    prices
) p
where price BETWEEN 1 AND 100

答案 2 :(得分:1)

您需要在WHERE子句中重复CASE语句,如下所示:

WHERE 
    CASE
       WHEN salePriceDate BETWEEN salePriceStartDate AND salePriceEndDate
          THEN salePrice
          ELSE 
             CASE
                WHEN salePriceDate2 BETWEEN salePriceStartDate2 AND salePriceEndDate2
                   THEN salePrice2
                   ELSE normalPrice
             END
    END  BETWEEN 1 AND 100

或者,将原始查询包装在内部查询中:

SELECT * FROM 
( SELECT
    CASE
       WHEN salePriceDate BETWEEN salePriceStartDate AND salePriceEndDate
          THEN salePrice
          ELSE 
             CASE
                WHEN salePriceDate2 BETWEEN salePriceStartDate2 AND salePriceEndDate2
                   THEN salePrice2
                   ELSE normalPrice
             END
    END AS price
FROM 
    prices
) AS a
WHERE 
    price BETWEEN 1 AND 100

答案 3 :(得分:1)

您可以先将查询选为一组,然后应用where子句。

select price from (SELECT
    CASE
       WHEN salePriceDate BETWEEN salePriceStartDate AND salePriceEndDate
          THEN salePrice
          ELSE 
             CASE
                WHEN salePriceDate2 BETWEEN salePriceStartDate2 AND salePriceEndDate2
                   THEN salePrice2
                   ELSE normalPrice
             END
    END AS price
FROM 
    prices) Records
WHERE 
    price BETWEEN 1 AND 100

答案 4 :(得分:1)

您可以使用公用表表达式按计算字段

进行过滤
with cte as (
    SELECT
        CASE
           WHEN salePriceDate BETWEEN salePriceStartDate AND salePriceEndDate
              THEN salePrice
              ELSE 
                 CASE
                    WHEN salePriceDate2 BETWEEN salePriceStartDate2 AND salePriceEndDate2
                       THEN salePrice2
                       ELSE normalPrice
                 END
        END AS price
    FROM 
        prices
)
select * from cte 
where price BETWEEN 1 AND 100