The select clause is the last clause that should be executed logically, with the exception of order by . The having occurs before the selection, so aliases are not yet available.
If you really want to use an alias, and not what I would recommend doing this, you can use the built-in view to get the available aliases:
select StoreId, _count from (select Store_id as StoreId, count(*) as _count from StoreProduct group by Store_id) T where _count > 0
Or in SQL Server 2005 and above, CTE:
; with T as (select Store_id as StoreId, count(*) as _count from StoreProduct group by Store_id) select StoreId, _count from T where _count > 0
Shannon Severance Jan 15 '10 at 0:52 2010-01-15 00:52
source share