头歌平台云盘算实验

北冰洋以北  金牌会员 | 2024-6-28 00:31:33 | 来自手机 | 显示全部楼层 | 阅读模式
打印 上一主题 下一主题

主题 582|帖子 582|积分 1746

其他部分答案在B站工房 https://gf.bilibili.com/item/detail/1105242061



Hive综合应用案例——用户学历查询

1 查询每一个用户从出生到现在的总天数

  1. ---------- 禁止修改 ----------
  2. drop database if exists mydb cascade;
  3. ---------- 禁止修改 ----------
  4. ---------- begin ----------
  5. ---创建mydb数据库
  6. create database if not exists mydb;
  7. ---使用mydb数据库
  8. use mydb;
  9. ---创建表user
  10. create table usertab(
  11.     id string,
  12.     sex string,
  13.     time string,
  14.     education string,
  15.     occupation string,
  16.     income string,
  17.     area string,
  18.     desired_area string,
  19.     city_countryside string
  20. )
  21. row format delimited fields terminated by ',';
  22. ---导入数据:/root/data.txt
  23. load data local inpath '/root/data.txt' into table usertab;
  24. --查询每一个用户从出生到2019-06-10的总天数
  25. select id, datediff('2019-06-10',regexp_replace(time, '/', '-')) from usertab;
复制代码
2 同一个地区雷同的教育程度的最高收入

  1. ---------- 禁止修改 ----------
  2. drop database if exists mydb cascade;
  3. ---------- 禁止修改 ----------
  4. ---------- begin ----------
  5. --创建mydb数据库
  6. create database if not exists mydb;
  7. ---使用mydb数据库
  8. use mydb;
  9. ---创建表user
  10. create table usertab1(
  11.     id int,
  12.     sex string,
  13.     time string,
  14.     education string,
  15.     occupation string,
  16.     income string,
  17.     area string,
  18.     desired_area string,
  19.     city_countryside string
  20. )
  21. row format delimited fields terminated by ',';
  22. ---导入数据:/root/data.txt
  23. load data local inpath '/root/data1.txt' into table usertab1;
  24. --同一个地区相同的教育程度的最高收入
  25. select area,education,income
  26. from(
  27.     select area,education,income,
  28.     row_number() over(
  29.         partition by area, education order by income desc
  30.     ) as t1
  31.     from usertab1
  32. ) as t2
  33. where t2.t1 = 1;
  34. ---------- end ----------
复制代码
3 统计各级学历所占总人数百分比

  1. ---------- 禁止修改 ----------
  2. drop database if exists mydb cascade;
  3. set hive.mapred.mode=nonstrict;
  4. ---------- 禁止修改 ----------
  5. ---------- begin ----------
  6. --创建mydb数据库
  7. create database if not exists mydb;
  8. ---使用mydb数据库
  9. use mydb;
  10. ---创建表user
  11. create table usertab2(
  12.     id int,
  13.     sex string,
  14.     time string,
  15.     education string,
  16.     occupation string,
  17.     income string,
  18.     area string,
  19.     desired_area string,
  20.     city_countryside string
  21. )
  22. row format delimited fields terminated by ',';
  23. ---导入数据:/root/data.txt
  24. load data local inpath '/root/data.txt' into table usertab2;
  25. --统计各级学历所占总人数百分比(对结果保留两位小数)
  26. select concat(round(t1.cnted * 100 / t2.cnt, 2),'%'), t1.education
  27. from
  28.     (
  29.         select count(*) as cnted,education
  30.         from usertab2
  31.         group by education
  32.     ) as t1,
  33.     (
  34.         select count(*) as cnt from usertab2
  35.     ) as t2
  36. order by t1.education;
  37. ---------- end ----------
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

北冰洋以北

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表