09- 15 hivesql 基础

hivesql 基础:

hive简介:
hive是基于hadoop的数据仓库

mapreduce简介:

基础语法:
查询语句:select a from b where c ;
Groupby 分组
Order by 排序

执行顺序:

From(先读取表)-> where (筛选)–> group by (聚合分组)-> having-> select(选择) order by(排序)

Group by 执行顺序在select之前(先分组再查询);

常用函数:

1、 如何把时间戳转化为日期

2、 如何计算日期间隔

截取时间函数:
dt=2019-01-01 19:55:55
to_data(dt)=2019-01-01
year(dt)= 2019 截取年
month(dt)=01 截取月
hour(dt)=19

substr( string f,int start ,int end) 字符串截取函数
substr(dt,1,10) =2019-01-01
substr(dt,6) 截取从第六位到最后一位;

3、 条件函数

前提两个表:user_info用户信息表,user_trade 用户交易表:

3-1 、case when

Eg: 统计四个年龄段20岁以下,2030岁,3040岁,40岁以上的用户数:
Select case when age<20 then ‘20岁以下’
When age>=20 and age<30 then ‘20-30岁’
When age>=30 and age<40 then ’30-40岁’
Else ‘40岁以上’ end as age_type,
Count(distinct user_id)user_num 这一句是对user_id 去重
From user_info
Group by case when age<20 then ‘20岁以下’
When age>=20 and age<30 then ‘20-30岁’
When age>=30 and age<40 then ’30-40岁’
Else ‘40岁以上’ end as age_type;

注意:group by 执行在select之前,所以后边的重命名不生效,只能在select后边重命名。

3-2 、if

统计每个性别用户等级高低的分别情况(level>5位高级)
分类汇总的字段有两个:性别,高低等级
Select sex , if(level>5, ’高’ , ’低’) as level_type,
Count(distinct user_id) user_num
From user_info
Group by sex,if(level>5 , ‘高’,’低’);
解析: if中条件满足则注释为高,否则注释为低;

4、 字符串函数
4-1

Substr( string a, int start ,int end)

每个月新激活的用户:
Select substr(firstactivetime,1,7) as month,
Count(distinct user_id) user_num
From user_info
Group by substr( firstactivetime ,1,7);

4-2
Substr( string a , int start ,int len) 若不指定截取长度,则截止末尾;

4-3

Get_json_object( stringjson_string, string path)

Param1: 需要解析的json字段
Param2:用key取出想要获取的value值

Extra1(string) (“sysytemtyoe”:”ios”,”education”:”master”,”marriage_status”:”1”,”phonebrand”:”iphonex”)
Extra2(map<string:string “sysytemtyoe”:”ios”,”education”:”master”,”marriage_status”:”1”,”phonebrand”:”iphonex”)

对于键值对类型数据,有些公司以string形式存储,有些公司存储map型,不同类型的数据处理需要使用不同的字符串函数;

方法1、select get_json_object(extra1 ,’ . p h o n e b r a n d ’ ) a s p h o n e b r a n d , C o u n t ( d i s t i n c t u s e r

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值