I am currently playing with the Northwind Sample database that ships with SQL Server. I am trying to write a query for finding “Customers who has spent more than $25,000 in orders with Northwind”. In the same context I have written the following query:
select a.customerid, sum (b.unitprice*b.quantity) as Total_Amount
from orders a join [order details] b on a.orderid=b.orderid
--where Total_Amount > 25000 --ERROR: In this filter condition, SQL Server is not recongnizing "Total_Amount"
group by a.customerid
order by Total_Amount desc
The query seems to be working correctly except for the fact that I am unable to put the filter condition (of 25000, pls refer to the commented line in the query) as it seems SQL Server does not recognize alias names (i.e.,Total_Amount), in the filter condition. However it is recognizing the alias in the Order By clause.
Pls help me with the right approach to this query and what would be the possible work arounds?
Thanks,
XM
1 Answer