How do I reference an alias in a WHERE clause?

浪尽此生 提交于 2020-01-14 13:43:09

问题


Here's my statement:

SELECT 
  C.Account, 
  (RTRIM(N.FIRST) + ' ' + RTRIM(LTRIM(N.MIDDLE)) + ' ' + RTRIM(LTRIM(N.LAST)) + ' ' + LTRIM(N.SUFFIX)) AS OwnerName,
  DateAdd(dd, -1, C.ExpirationDate) as RealExpirationDate, 
  C.Description, 
  C.Type
FROM CARD as C
  INNER JOIN NAME as N ON C.Account = N.Account
WHERE (RealExpirationDate BETWEEN @StartDate AND @EndDate)
  AND C.Type IN(10,15,17,25)

I keep getting an error saying that RealExpirationDate is an invalid column name. How can I reference that alias?


回答1:


You can't in your code above, remember WHERE happens before SELECT, so you'd have to use:

WHERE DateAdd(dd, -1, C.ExpirationDate) BETWEEN @StartDate AND @EndDate

The most common way to alias something like this would be some inner view / query like so:

SELECT
  n.FooBar,  --here we can use FooBar
  t.BarFoo
FROM
  MyTable t
INNER JOIN
(
 SELECT
   myTestCase as FooBar
 From MyTable2
) n



回答2:


You actually shouldn't try to reuse the alias in this case. It isn't sargable (Can't do a range seek on ExpirationDate).

Just use

WHERE C.ExpirationDate 
  BETWEEN DateAdd(dd, 1, @StartDate)  AND DateAdd(dd, 1, @EndDate)



回答3:


You can't reference an alias. Your query would have to be

SELECT 
  C.Account, 
  (RTRIM(N.FIRST) + ' ' + RTRIM(LTRIM(N.MIDDLE)) + ' ' + RTRIM(LTRIM(N.LAST)) + ' ' + LTRIM(N.SUFFIX)) AS OwnerName,
  DateAdd(dd, -1, C.ExpirationDate) as RealExpirationDate, 
  C.Description, 
  C.Type
FROM CARD as C
  INNER JOIN NAME as N ON C.Account = N.Account
WHERE (DateAdd(dd, -1, C.ExpirationDate) BETWEEN @StartDate AND @EndDate)
  AND C.Type IN(10,15,17,25)



回答4:


In SQL Server you can do this using CROSS APPLY. This is simpler than a subselect, but I'm not sure if there is a performance difference.

SELECT 
  C.Account, 
  (RTRIM(N.FIRST) + ' ' + RTRIM(LTRIM(N.MIDDLE)) + ' ' + RTRIM(LTRIM(N.LAST)) + ' ' + LTRIM(N.SUFFIX)) AS OwnerName,
  RealExpirationDate, 
  C.Description, 
  C.Type
FROM CARD as C
  INNER JOIN NAME as N ON C.Account = N.Account
CROSS APPLY
  (SELECT DateAdd(dd, -1, C.ExpirationDate)) CrossA(RealExpirationDate)
WHERE (RealExpirationDate BETWEEN @StartDate AND @EndDate)
  AND C.Type IN(10,15,17,25)



回答5:


Use:

SELECT 
  C.Account, 
  (RTRIM(N.FIRST) + ' ' + RTRIM(LTRIM(N.MIDDLE)) + ' ' + RTRIM(LTRIM(N.LAST)) + ' ' + LTRIM(N.SUFFIX)) AS OwnerName,
  DateAdd(dd, -1, C.ExpirationDate) as RealExpirationDate, 
  C.Description, 
  C.Type
FROM CARD as C
INNER JOIN NAME as N ON C.Account = N.Account
WHERE (DateAdd(dd, -1, C.ExpirationDate) BETWEEN @StartDate AND @EndDate)
AND C.Type IN(10,15,17,25)

or

SELECT * from (
SELECT C.Account, 
  (RTRIM(N.FIRST) + ' ' + RTRIM(LTRIM(N.MIDDLE)) + ' ' + RTRIM(LTRIM(N.LAST)) + ' ' + LTRIM(N.SUFFIX)) AS OwnerName,
  DateAdd(dd, -1, C.ExpirationDate) as RealExpirationDate, 
  C.Description, 
  C.Type
FROM CARD as C
INNER JOIN NAME as N ON C.Account = N.Account
WHERE C.Type IN(10,15,17,25)
) t
WHERE RealExpirationDate BETWEEN @StartDate AND @EndDate


来源:https://stackoverflow.com/questions/9080128/how-do-i-reference-an-alias-in-a-where-clause

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!