大数据之hive 基础查询

本文通过SQL查询分析了用户信息和交易数据,包括:激活天数超500天的男女用户分布、不同性别和教育程度的用户比例、2019年前4个月各时段商品类别购买金额分布以及2018年购买多个商品品类的用户数量。这些数据揭示了用户行为模式和电商销售趋势。
摘要由CSDN通过智能技术生成

/*电商项目基础查询
温馨提示:本任务使用的云试验平台为上课时所用,非电商项目实验平台,数据库名 kaikeba
先使用 desc 表名;语句查看字段,在进行查询。

  1. 激活天数距今超过 500 天的男女分布情况(使用 user_info)(使用 user_info 表)
  2. 不同性别、教育程度的用户分布情况(使用 user_info) (使用 user_info 表)
  3. 2019 年前 4 个月中,全天各个时间段的不同品类购买金额分布(24 小时制,使用 user_trade 表)
  4. 统计在 2018 年购买的商品品类在两个以上的用户数(使用 user_trade 表)*/

– 1. 激活天数距今超过 500 天的男女分布情况(使用 user_info)(使用 user_info 表)
/*
1.现将激活天数计算
2.再将性别分段
3.根据性别分段统计用户个数
/
select sex,count(distinct user_id)
from user_info
where datediff(current_date,to_date(firstactivetime))>500
group by sex;
– 2. 不同性别、教育程度的用户分布情况(使用 user_info) (使用 user_info 表)
/

1.将性别和教育程度分段
2.在统计各个分段的用户数
get_json_object(string json_string, string path)
解析json字符串的一个字段
第一个参数填写json对象变量,第二个参数使用$表示json变量标识,
然后用 . 或 [] 读取对象或数组;如果输入的json字符串无效,那么返回NULL。
每次只能返回一个数据项。
*/

select sex,get_json_object(extra1,’ . e d u c a t i o n ′ ) , c o u n t ( d i s t i n c t u s e r i d ) f r o m u s e r i n f o g r o u p b y s e x , g e t j s o n o b j e c t ( e x t r a 1 , ′ .education'), count(distinct user_id) from user_info group by sex,get_json_object(extra1,' .education),count(distinctuserid)fromuserinfogroupbysex,getjsonobject(extra1,.education’);
– 方法二
select sex,extra2[‘education’],count(user_id)
from user_info
group by sex,extra2[‘education’];

– 3. 2019 年前 4 个月中,全天各个时间段的不同品类购买金额分布(24 小时制,使用 user_trade 表)
/*
substr(string A,int startindex,intlen),
1.先将将2019年的前4个月限定条件
2.再将不同购买品类分段
3.统计不同品类的购买金额
字符串的截取,如果不指定len,则从起始位置截取到最后。
*/
select substr(from_unixtime(pay_time,‘yyyy-MM-dd hh’),12),goods_category,sum(pay_amount)
from user_trade
where dt between ‘2019-01’ and ‘2019-04’
group by substr(from_unixtime(pay_time,‘yyyy-MM-dd hh’),12),goods_category;

– 4. 统计在 2018 年购买的商品品类在两个以上的用户数(使用 user_trade 表)
/*
1.先分析2018年的用户购买的商品种类,在统计种类大于2的用户
2.用子查询来统计购买商品种类大于2的所有用户数*/

select count(distinct a.user_name)
from(select user_name
from user_trade
where year(dt)=‘2018’
group by user_name
having count(distinct goods_category)>2
)a;

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值