今天用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

最新文章

  1. 使用po模式读取豆瓣读书最受关注的书籍,取出标题、评分、评论、题材 按评分从小到大排序并输出到txt文件中
  2. 窥探Swift之数组安全索引与数组切片
  3. MySQL大小写补坑记
  4. 【Go入门教程3】流程(if、goto、for、switch)和函数(多个返回值、变参、传值与传指针、defer、函数作为值/类型、Panic和Recover、main函数和init函数、import)
  5. 玉伯的一道课后题题解(关于 IEEE 754 双精度浮点型精度损失)
  6. C# 判断是否联网
  7. over分析函数
  8. 不同系统间传输float型数据
  9. [Firebase + PWA] Keynote: Progressive Web Apps on Firebase
  10. SQL 把数据从一张表更新到另一张表
  11. java web中jsp连接mysql数据库 以及数据库连接池的使用
  12. 实现自己的http server - loop_in_codes - C++博客
  13. easyui 时间段校验,开始时间小于结束时间,并且时间间隔不能超过30天
  14. HDU 6143 Killer Names
  15. php的接口,抽象类,trait(转载,合并)
  16. ABP实践(3)-ASP.NET Core 2.x版本(从创建实体到输出api)简单实现商品列表及增删改
  17. Gradle学习笔记(1)创建简单的Java项目
  18. [多线程]多线程(Thread、Runnable、Callable)
  19. centos 共享文件目录
  20. mysql 5.17 的update失败问题

热门文章

  1. zoj2318
  2. 洛谷——P1586 四方定理
  3. CentOS 7.1 中文正式版下载 - 最流行的免费开源企业级 Linux 服务器操作系统
  4. [BZOJ 1150] 数据备份
  5. 【枚举】bzoj1800 [Ahoi2009]fly 飞行棋
  6. Problem C: 零起点学算法18——3个数比较大小
  7. linux-排序-sort
  8. 使用GIT时排除NuGet的packages文件夹
  9. winform TreeView树节点上下移动
  10. vs2013 编译 notepad++ 源代码