当前位置: 首页 > news >正文

建设网站总结新闻发布最新新闻

建设网站总结,新闻发布最新新闻,成都网站建设 seo,qq是谁开发的注:参考文章: HiveSql面试题10--sum(if)统计问题_hive sum if-CSDN博客文章浏览阅读5.8k次,点赞6次,收藏19次。0 需求分析t_order表结构字段名含义oid订单编号uid用户idotime订单时间(yyyy-MM-dd)oamount订…

注:参考文章:

HiveSql面试题10--sum(if)统计问题_hive sum if-CSDN博客文章浏览阅读5.8k次,点赞6次,收藏19次。0 需求分析t_order表结构字段名含义oid订单编号uid用户idotime订单时间(yyyy-MM-dd)oamount订单金额(元)所有在2018年1月下过单并且在2月没有下过单的用户,在3月份的下单情况:目标字段名含义_hive sum ifhttps://blog.csdn.net/godlovedaniel/article/details/108325219

0  需求分析

t_order表结构如图:

  要求:t_order表扫描次数不超过2次的前提下,统计所有在2018年1月下过单且在2月份没有下过单的用户,该用户在3月份的下单情况:

1 数据准备

CREATE TABLE t_order (oid int ,uid int ,otime string,oamount int)
ROW format delimited FIELDS TERMINATED BY ",";
load data local inpath "/opt/module/hive_data/t_order.txt" into table t_order;

2  数据分析

完整代码为:

with tmp as (selectoid,uid,otime,date_format(otime, 'yyyy-MM') as                                                  dt,oamount,---计算rk的目的是为了获取记录中的第一条row_number() over (partition by uid,date_format(otime, 'yyyy-MM') order by otime) rk,--- 计算cnt的目的是为了获取记录中的最后一条count(*) over (partition by uid,date_format(otime, 'yyyy-MM'))                    cntfrom t_orderorder by uid
)
selectuid,--每个用户一月份的订单数sum(if(dt = '2018-01', 1, 0)) as                 m1_count,--每个用户二月份的订单数sum(if(dt = '2018-02', 1, 0)) as                 m2_count,--每个用户三月份的订单数(当月订单金额超过10元的订单个数)sum(if(dt = '2018-03' and oamount > 10, 1, 0))   m3_count,--当月(3月份)首次下单的金额sum(if(dt = '2018-03' and rk = 1, oamount, 0))   m3_first_amount,--当月(3月份)末次下单的金额(rk =cnt小技巧)sum(if(dt = '2018-03' and rk = cnt, oamount, 0))  m3_last_amount
from tmp
group by uid
--将下单记录转化成下单次数判断
having m1_count >0 and m2_count=0;

 最终的输出结果为:

上述代码解析:

step1: 用date_format函数进行日期格式化,row_number() over() 获得排名rk, count(*)over()获得统计值cnt

selectoid,uid,otime,date_format(otime, 'yyyy-MM') as  dt,oamount,---计算rk的目的是为了获取记录中的第一条row_number() over (partition by uid,date_format(otime, 'yyyy-MM') order by otime) rk,--- 计算cnt的目的是为了获取记录中的最后一条count(*) over (partition by uid,date_format(otime, 'yyyy-MM'))  cntfrom t_order

step2:

  • 获取当月订单金额超过10元的订单个数 :sum(if(条件, 1, 0)) 或者 sum( case when 条件 then 1 else 0 end  );
  • 获取当月首次下单金额:rk=1
  • 获取当月末次下单金额:rk=cnt (每个分组的记录数cnt 同时也等于分组内,最后一条记录数的排序值rk)
with tmp as (selectoid,uid,otime,date_format(otime, 'yyyy-MM') as                                                  dt,oamount,---计算rk的目的是为了获取记录中的第一条row_number() over (partition by uid,date_format(otime, 'yyyy-MM') order by otime) rk,--- 计算cnt的目的是为了获取记录中的最后一条count(*) over (partition by uid,date_format(otime, 'yyyy-MM'))                    cntfrom t_orderorder by uid
)
selectuid,--每个用户一月份的订单数sum(if(dt = '2018-01', 1, 0)) as                 m1_count,--每个用户二月份的订单数sum(if(dt = '2018-02', 1, 0)) as                 m2_count,--每个用户三月份的订单数(当月订单金额超过10元的订单个数)sum(if(dt = '2018-03' and oamount > 10, 1, 0))   m3_count,--当月(3月份)首次下单的金额sum(if(dt = '2018-03' and rk = 1, oamount, 0))   m3_first_amount,--当月(3月份)末次下单的金额(rk =cnt小技巧)sum(if(dt = '2018-03' and rk = cnt, oamount, 0))  m3_last_amount
from tmp
group by uid
having m1_count >0 and m2_count=0;

3 小结

   本案例用到的知识点:

  • sum(if()) 有条件累加;
  •  row_number() over(partition by ....order by ..) 排序,求分组topN
  •  count(*) over(partition by ...) 分组统计记录数。每组的记录数同时也是最后一条记录的排序值。
  • 将下单记录转化成下单次数判断 m1_count >0 and m2_count=0;
http://www.hkea.cn/news/119241/

相关文章:

  • 佛山h5建站模板怎样优化网站
  • 第三方做公司网站谷歌搜索广告优化
  • 网站风格模板快速排名精灵
  • 做网站横幅 的网站推荐几个公司推广
  • html5国内网站建设客户管理软件
  • 网站建设报价单站长工具 seo查询
  • 日本电商网站贵州快速整站优化
  • 物业服务网站建设建立网站要多少钱一年
  • 中铁建设门户加长版廊坊百度提升优化
  • 最便宜的外贸网站建设电商平台运营方案
  • 做网站应该会什么问题网络营销软文范例500字
  • 摄影网课百度关键词优化查询
  • 打广告型的营销网站西安百度推广外包
  • 乌鲁木齐招聘网站建设一站式网络营销
  • 中小型网站建设服务淘宝数据分析工具
  • 梧州网站设计企业网站模板建站
  • 行政事业单位网站建设建议营销策划公司
  • 网络推广网站怎么做百度联盟广告点击一次收益
  • wordpress居中样式宁波seo网络推广外包报价
  • java做网站用到哪些技术网络营销的重要性与意义
  • 网络营销推广的作用谷歌seo什么意思
  • 免费网站建设解决方案郑州网络营销公司哪个好
  • 转转怎么做钓鱼网站税收大数据
  • 株洲专业网站排名优化深圳产品网络推广
  • 深圳美食教学网站制作如何免费搭建自己的网站
  • 兰州移动端网站建设广东整治互联网霸王条款
  • 彩票网站该怎么建设天津seo实战培训
  • 原平的旅游网站怎么做的新冠疫情最新情况最新消息
  • 网站开发软件著作权归谁seo外包
  • 小说网站的网编具体做哪些工作南宁网站快速排名提升