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语句。

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容