HiveSQL基本查询分析练习

**

HiveSQL基本查询分析练习

**

**

需求1:某次经营活动中,商家发起了“异性拼团购”活动,选出城市在北京,性别为“女”的十名用户。

SELECT user_name
FROM user_info
WHERE city='beijing' and sex='female'
limite 10;

需求2:选出在2019年6月18日,购买商品品类是food的用户名,购买数量,支付金额。

SELECT user_name,
	   piece,
	   pay_amount
FROM user_trade
WHERE dt='2019-06-18' and goods_category='food';

需求3:2019年一月到三月,每个品类有多少人购买,累计金额是多少。

SELECT goods_category,
       count(distinct user_name) as user_num,
       sum(pay_amount) as total_amount
FROM user_trade
WHERE dt between '2019-01-01' and '2019-03-31'
GROUP BY goods_category;

需求4-1:2019年4月,支付金额超过5万元的用户。

SELECT user_name,
       sum(pay_amount) as total_amount
FROM user_trade
WHERE dt between '2019-04-01' and '2019-04-30'
GROUP BY user_name
HAVING sum(pay_amount)>50000;

需求4-2:2019年4月,支付金额top5用户。(order by执行顺序在select之后,所以需要使用重新定义的列名进行排序)

SELECT user_name,
       sum(pay_amount) as total_amount
FROM user_trade
WHERE dt between '2019-04-01' and '2019-04-30'
GROUP BY user_name
ORDER BY total_amount DESC
limit 5;

需求5:用户的首次激活时间与2019年5月1日的时间间隔。

SELECT user_name,
       datediff('2019-05-01',to_date(firstactivetime))
FROM user_info;

需求6:对用户的年龄段进行分析,统计20岁以下,20到30,30到40,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) as user_num
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;

需求7:统计每个性别用户等级高低的分布情况。

SELECT sex,
       if(level>5,'高','低') as level_type,
       count(distinct user_id) as user_num
FROM user_info
GROUP BY sex,
         if(level>5,'高','低');

需求8:分析每个月的拉新情况。

SELECT substr(firstactivetime,1,7) as month,
       count(distinct user_id) user_num
FROM user_info
GROUP BY substr(firstactivetime,1,7);

需求9:不同手机品牌的用户数:

SELECT substr(extra2['phonebrand'],1,6) as phone_brand,
       count(distinct user_id) user_num
FROM user_info
GROUP BY substr(extra2['phonebrand'],1,6);

需求10:ELLA用户的2018年平均每次支付金额,以及最大最小支付日期的间隔:

SELECT avg(pay_amount) as avg_amount,
       datediff(max(from_unixtime(pay_time,'yyyy-MM-dd')),min(from_unixtime(pay_time,'yyyy-MM-dd'))) as day
FROM user_trade
WHERE year(dt)='2018' and user_name='ELLA';

需求11:2018年购买的商品品类在两个以上的用户。

SELECT count(a.user_name)
FROM
    (SELECT user_name,
            count(distinct goods_category)as category_num
     FROM user_trade
     WHERE year(dt)='2018'
     GROUP BY user_name
     HAVING count(distinct goods_category)>2) a;

需求12:用户激活时间在2018年,年龄段在20-30岁和30-40岁的婚姻状况分布。

SELECT a.age_type,
       if(a.marriage_status=1,'已婚','未婚'),
       count(distinct a.user_id)
FROM
       (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,
               get_json_object(extra1,'$.marriage_status') as marriage_status,
               user_id
        FROM user_info
        WHERE to_date(firstactivetime) between '2018-01-01' and '2018-12-31') a
WHERE a.age_type in ('20-30岁','30-40岁')
GROUP BY a.age_type,
         if(a.marriage_status=1,'已婚','未婚');

需求13:激活天数距今超过300天的男女分布情况。

SELECT sex,
       count(distinct user_id)
FROM user_info
WHERE datediff(current_date(),to_date(firstactivetime))>300
GROUP BY sex;

需求14:不同性别,教育程度的分布情况。

SELECT sex,
       extra2['education'] as education,
       count(distinct user_id)
FROM user_info
GROUP BY sex,
         extra2['education'];

需求15:2019年1月1日到2019年4月30日,每个时段的不同品类购买金额分布。

SELECT substr(from_unixtime(pay_time,'yyyy-MM-dd HH'),12),
       goods_category,
       sum(pay_amount)
FROM user_trade
WHERE dt between '2019-01-01' and '2019-04-30'
GROUP BY substr(from_unixtime(pay_time,'yyyy-MM-dd HH'),12),
               goods_category;

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1.上传tar包 2.解压 tar -zxvf hive-1.2.1.tar.gz 3.安装mysql数据库 推荐yum 在线安装 4.配置hive (a)配置HIVE_HOME环境变量 vi conf/hive-env.sh 配置其中的$hadoop_home (b)配置元数据库信息 vi hive-site.xml 添加如下内容: javax.jdo.option.ConnectionURL jdbc:mysql://localhost:3306/hive?createDatabaseIfNotExist=true JDBC connect string for a JDBC metastore javax.jdo.option.ConnectionDriverName com.mysql.jdbc.Driver Driver class name for a JDBC metastore javax.jdo.option.ConnectionUserName root username to use against metastore database javax.jdo.option.ConnectionPassword hadoop password to use against metastore database 5.安装hive和mysq完成后,将mysql的连接jar包拷贝到$HIVE_HOME/lib录下 如果出现没有权限的问题,在mysql授权(在安装mysql的机器上执行) mysql -uroot -p #(执行下面的语句 *.*:所有库下的所有表 %:任何IP地址或主机都可以连接) GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'root' WITH GRANT OPTION; FLUSH PRIVILEGES; 6. Jline包版本不一致的问题,需要拷贝hive的lib录中jline.2.12.jar的jar包替换掉hadoop中的 /home/hadoop/app/hadoop-2.6.4/share/hadoop/yarn/lib/jline-0.9.94.jar 启动hive bin/hive ---------------------------------------------------------------------------------------------------- Hive几种使用方式: 1.Hive交互shell bin/hive 2.Hive JDBC服务(参考java jdbc连接mysql) 3.hive启动为一个服务器,来对外提供服务 bin/hiveserver2 nohup bin/hiveserver2 1>/var/log/hiveserver.log 2>/var/log/hiveserver.err & 启动成功后,可以在别的节点上用beeline去连接 bin/beeline -u jdbc:hive2://mini1:10000 -n root 或者 bin/beeline ! connect jdbc:hive2://mini1:10000 4.Hive命令 hive -e ‘sql’ bin/hive -e 'select * from t_test'
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值