hive中sql执行顺序
- from
- join on 或 lateral view explode(需炸裂的列) tbl as 炸裂后的列名
- where
- group by (开始使用select中的别名,从group 开始往后都可用)
- 聚合函数 如Sum() avg() count(1)等
- having
- select 中若包含over()开窗函数,执行完非开窗函数后select等待执行完开窗函数,然后执行select完,开窗函数通过表数据进行分区和排序,跟select查询中的字段是平行关系,不依赖查询字段。
- distinct
- order by
- limit
例子:
uid 用户id, day 登陆日期
需求: 求20年9月份 登陆天数超过15的前10个用户。
select uid, count(*) as cnt from tb
where substring(day, 1, 7) = '2020-09'
group by uid
having cnt >= 15
order by uid desc
limit 10;