Here is my example

SQLfiddle.com.

Could anybody explain why AvgQuant is invalid column name?

Also SQLfiddle is great tool, but how do I set rounding settings? For example, rounding is not correct in the following example (just remove where clause)

SELECT [Location]
  ,Avg([Quantity]) over (partition BY LOCATION) AS AvgQuant
  ,[Street]
FROM tblExample;

Regards

有帮助吗?

解决方案

It is because WHERE clause executes before projection thus the alias isn't available in this phase. You must use subquery to achieve what you want.

SELECT *
FROM
(
  SELECT [Location]
      ,Avg([Quantity]) over (partition BY LOCATION) AS AvgQuant
      ,[Street]
  FROM tblExample
) A
WHERE AvgQuant between 3.5 and 5.5;
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top