having语句使用

错误示例
select hotel_seq,count(poi_name) as poi_num
from dm_international_hotel_poi_within5km
where dt=20180722
and poi_num>10
group by 1

报错信息:
Error while compiling statement: FAILED: SemanticException [Error 10004]: Line 1:108 Invalid table alias or column reference 'poi_num': (possible column names are: hotel_seq, city_code, country_name, hotel_grade, poi_id, poi_name, category, distance, overall_rank, category_rank, dt)

正确示例
select hotel_seq,count(poi_name) as poi_num
from dm_international_hotel_poi_within5km
where dt=20180722
group by 1
having poi_num>10
解释

如果你对何时应该使用WHERE,何时使用HAVING仍旧很迷惑,请遵照下面的说明:
WHERE语句在GROUPBY语句之前;SQL会在分组之前计算WHERE语句。
HAVING语句在GROUPBY语句之后;SQL会在分组之后计算HAVING语句。

你可能感兴趣的:(having语句使用)