摘要:
Select字句在逻辑上是SQL语句最后进行处理的最后一步,所以,以下查询会发生错误:SELECT YEAR(OrderDate) AS OrderYear, COUNT(DISTINCT CustomerID) AS NumCustsFROM dbo.OrdersGROUP BY OrderYear;因为group by是在Select之前进行的,那个时候orderYear这个列并没有形成。如果要查询成功,可以像下面进行修改:SELECT OrderYear, COUNT(DISTINCT CustomerID) AS NumCustsFROM (SELECT YEAR(OrderDate) 阅读全文