hive 用户订单行为 基础操作
2024-08-29 02:50:27
今天用hive查询用户日志表.这是日志表的格式:
user_id,item_id,cat_id,merchant_id,brand_id,month,day,action,age_range,gender,province
328862,323294,833,2882,2661,8,29,0,0,1,内蒙古
328862,844400,1271,2882,2661,8,29,0,1,1,山西
328862,575153,1271,2882,2661,8,29,0,2,1,山西
328862,996875,1271,2882,2661,8,29,0,1,1,内蒙古
328862,1086186,1271,1253,1049,8,29,0,0,2,浙江
328862,623866,1271,2882,2661,8,29,0,0,2,黑龙江
328862,542871,1467,2882,2661,8,29,0,5,2,四川
328862,536347,1095,883,1647,8,29,0,7,1,吉林
328862,364513,1271,2882,2661,8,29,0,1,2,贵州
328862,575153,1271,2882,2661,8,29,0,0,0,陕西
创建数据库名:
create database hive;
创建表名:
CREATE TABLE hive.user_log(user_id INT,item_id INT,cat_id INT,merchant_id INT,brand_id INT,month STRING,day STRING,action INT,age_range INT,gender INT,province STRING) COMMENT 'Welcome to xmu dblab,Now create hive.user_log!' ROW FORMAT DELIMITED FIELDS TERMINATED BY ',' STORED AS TEXTFILE LOCATION '/user/hive/user_log/user_log';
(1)查询10个交易记录:
select * from user_log limit 10;
(2)对于复杂的列名,可以使用别名:
select merchant_id as meri from user_log;
(3)使用嵌套语句:
select ul.meri from (select merchant_id as meri from user_log) as ul limit 10;
(4)统计有多少条行数据:
select count(*) from user_log;
(5)统计不重复的数据:
select count(distinct user_id) from user_log;
(6)使用group by 查询不重复的数据:
select count(*) from (select user_id,item_id,cat_id,merchant_id,brand_id,action on from user_log group by user_id,item_id,cat_id,merchant_id,brand_id,action having count(*)=1)a;
(7)查询某一天多少人购买了产品:
select count(distinct user_id) from user_log where action='2' and month='11' and day='11';
action=’2’ 表示支付,action=’1’表加入购物车:
(8)查询某一天男女购买的比例:
select count(*) from user_log where gender=0 and month='11' and day='11';
select count(*) from user_log where gender=1 and month='11' and day='11';
(9)查询某天某商品的购买用户,且某用户购买2次以上:
select user_id from user_log where action='2' group by user_id having count(action='2')>1;
(10)查询某品牌商品的浏览次数:
select brand_id,count(action) from user_log where action='2' group by brand_id;
参考: http://dblab.xmu.edu.cn/blog/1363-2/
https://blog.csdn.net/cafebar123/article/details/77206889
最新文章
- 使用po模式读取豆瓣读书最受关注的书籍,取出标题、评分、评论、题材 按评分从小到大排序并输出到txt文件中
- 窥探Swift之数组安全索引与数组切片
- MySQL大小写补坑记
- 【Go入门教程3】流程(if、goto、for、switch)和函数(多个返回值、变参、传值与传指针、defer、函数作为值/类型、Panic和Recover、main函数和init函数、import)
- 玉伯的一道课后题题解(关于 IEEE 754 双精度浮点型精度损失)
- C# 判断是否联网
- over分析函数
- 不同系统间传输float型数据
- [Firebase + PWA] Keynote: Progressive Web Apps on Firebase
- SQL 把数据从一张表更新到另一张表
- java web中jsp连接mysql数据库 以及数据库连接池的使用
- 实现自己的http server - loop_in_codes - C++博客
- easyui 时间段校验,开始时间小于结束时间,并且时间间隔不能超过30天
- HDU 6143 Killer Names
- php的接口,抽象类,trait(转载,合并)
- ABP实践(3)-ASP.NET Core 2.x版本(从创建实体到输出api)简单实现商品列表及增删改
- Gradle学习笔记(1)创建简单的Java项目
- [多线程]多线程(Thread、Runnable、Callable)
- centos 共享文件目录
- mysql 5.17 的update失败问题