Use an alias in the datediff & Order By statement?

Viewed 21

Why does the below code not work when I use the alias 'enddates' in the datediff (It throws up the error 'invalid column name: enddates')?

It works if I use the alias without the datediff (i.e. just ordering by enddates)

It also works if I copy the whole enddates formula into the datediff. But why can't I use the alias in the datediff? Thought the whole point of an alias is you can refer to it to shorten the code.

I did see something similar posted before, but there wasn't an answer

p.start_date,
(
  select min(p2.end_date) pend_date from projects p2 
  where p2.end_date not in
(select start_date from projects p3) and p2.end_date > p.start_date 
) enddates
from projects p 
where p.start_date not in 
(select end_date from projects) 
order by datediff(day, start_date,enddates), start_date```
0 Answers
Related