Question

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

Was it helpful?

Solution

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;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top