原因别名适用于 ORDER BY 但不适用于 WHERE

Reason alias works in ORDER BY but not in WHERE

我理解 I can't reference an alias in the WHERE clause,但这是为什么呢?是否有不同的解释?

这样的事情会产生错误:

declare @myTable table 
(
    num numeric(5,2),
    den numeric(5,2)
)
insert into @mytable 
select 1, 2
union
select 1, 3
union
select 2, 3
union
select 2, 4
union
select 2, 5
union
select null, 1

select num/den as 'calc' from @myTable
where calc is not null
order by calc

但是这 returns 行:

declare @myTable table 
(
    num numeric(5,2),
    den numeric(5,2)
)
insert into @mytable 
select 1, 2
union
select 1, 3
union
select 2, 3
union
select 2, 4
union
select 2, 5
union
select null, 1

select num/den as 'calc' from @myTable
--where calc is not null
order by calc

中所述,这是由于自然查询处理顺序:

  1. FROM
  2. ON
  3. OUTER
  4. WHERE
  5. GROUP BY
  6. CUBE | ROLLUP
  7. HAVING
  8. SELECT
  9. DISTINCT
  10. ORDER BY
  11. TOP