Hive SQL

HiveSQL

info 举例
id 1,2(唯一)
name a,b(唯一)
sex [male,famale]
age [13,70]
city beijing,shanghai
firstactivetime 2019-01-03 12:22:21
level [1,10]
extra1 string:{“systemtype”:“ios”,“education”:“master”,“marriage_status”:“1”,“phonebrand”:“iphone”}
extra2 {“systemtype”:“ios”,“education”:“master”,“marriage_status”:“1”,“phonebrand”:“iphone”}
trade 举例
name a,b
piece 购买数量
price 价格
pay 支付金额
goods 商品类型
time 1323334567 时间戳
dt partition ’2019-09-09
refund 举例
name a,b
piece 退款数量
money 退款金额
time 1323334567 时间戳
dt partition ’2019-09-09

一、HiveSQL
1,group by 分类汇总 常用搭配聚合函数,不允许嵌套组合avg(count(*))
2,group by …… having ,having对分组对象进行筛选,仅返回符合条件的结果
–2019年8月,支付金额超过5万云的用户–

 select 
    	name,sum(pay) as total_pay
    from trade 
    where dt between2019-08-01and2019-04-30group by name having sum(pay) > 50000

3,order by 进行排序 默认升序-asc,desc-降序,
4,写sql注意执行顺序
5,把时间戳转换为日期.日期转换为时间戳

--转换为日期
from_unixtime(bigint unixtime,'yyyy-MM-dd hh:mm:ss')

--转换为时间戳
unix_timestamp(string date)

6,时间间隔

datediff(string enddate,string startdate)
-- 日期增加函数
date_add(string startdate,int days)
--日期减少函数
date_sub(string startdate,int days)

7,条件函数

case when '条件' then '什么'
	 when '条件' then '什么'
	 when '条件' then '什么'
	 ……
else '什么' end

if('条件','真','假')

8,字符串函数

substr(string A,int start,int len)

9,复杂数据类型

--json格式的string
extra1(string)
{
  "systemtype":"ios","education":"master","marriage_status":"1","phonebrand":"iphone"}
获取:get_json_object(extra1,'$.education')

--map类型
extra2(map<string,string>)
{
  "systemtype":"ios","education":"master","marriage_status":"1","phonebrand":"iphone"}
获取:extra2['education']

--array类型
extra3(array(string))
获取:extra3[0]

--struct类型
extra4(struct<age:string,sex:string,addr:string>)
获取:extra4.addr    extra4.age

–练习–

激活天数距今超过300天的男女分布情况(使用info)
	
select sex,
 
	count(distinct id
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值