1

I get this error message from my SQL Server:

Invalid column name 'price'

in Microsoft SQL Server Management Studio.

My SQL statement:

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

How can I solve this?

tedi
  • 6,350
  • 5
  • 52
  • 67

5 Answers5

5

SQL does not allow you to use aliases defined in the SELECT in the WHERE clause (or most other clauses). This is part of the SQL language.

There are various ways to fix this. One method is a subquery. Another is a CTE. SQL Server also offers the lateral join:

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;

Also note that this simplifies the CASE expression. There is no need to nest CASE.

Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786
1

Please try this nested query. In this case, the calculated field will have the where clause rather having to do the case statement twice.

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
Ctznkane525
  • 7,297
  • 3
  • 16
  • 40
1

You need to repeat the CASE statement in the WHERE Clause like this:

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

Alternatively, wrap the original query in an inner query:

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
SchmitzIT
  • 9,227
  • 9
  • 65
  • 92
1

You could select the query first as a set and apply the where clause.

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
lucky
  • 12,734
  • 4
  • 24
  • 46
1

You can use Common Table Expression to filter by calculated field

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
SergeyA
  • 4,427
  • 1
  • 22
  • 15