Following query throws
Invalid column name WIP_Aging"
Query:
Select
ID, Assembly, Last_Accessed, DATEDIFF(day,Last_Accessed,GETDATE()) as WP_Aging
from
Details
where
WP_Aging >= 2
Unless you create a CTE or Sub-Select you can't refer to the aliased column name ( WP_Aging
) directly. Instead you have to repeat the expression:
Select
ID,
Assembly,
Last_Accessed,
DATEDIFF(day, Last_Accessed, GETDATE()) as WP_Aging
from
Details
where
DATEDIFF(day, Last_Accessed, GETDATE()) >= 2
Here's the CTE version:
;with cteAging as
(
Select
ID,
Assembly,
Last_Accessed,
WP_Aging = DATEDIFF(day, Last_Accessed, GETDATE())
from
Details
)
select
ID,
Assembly,
Last_Accessed,
WP_Aging
from
cteAging
where
WP_Aging >= 2
It's the way the engine works. The WHERE clause is done before the SELECT. You basically "created" a column named WP_aging during the SELECT. If you think about it it makes sense. You want to reduce the rows before you go and get the data want to see. So it does the WHERE and reduces the set first.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.