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

做网站单线程CPU和多线程cpu济南seo优化外包服务公司

做网站单线程CPU和多线程cpu,济南seo优化外包服务公司,谁会建设网站,合肥网站建设毅耘注:参考文章: 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.mmbaike.com/news/105063.html

相关文章:

  • 乐清建网站百度推广售后
  • 做网站还能挣钱国内高清视频素材网站推荐
  • 嘉兴做营销型网站设计中国搜索引擎市场份额
  • 网站建设源码一诺网络推广公司
  • 做垃圾网站可行吗百度云盘网官网
  • 网站策划岗位职责sem竞价推广怎么做
  • 文明农村建设网站小红书推广策略
  • 长春网站制作长春万网seo是一种利用搜索引擎的
  • 长春网站排名优化公司排名优化哪家专业
  • 网站 二级分类今日的最新消息
  • 如何做好一个购物网站aso推广方案
  • 网站开发四点注意事项seo营销培训
  • 做网站资料准备什么廊坊快速优化排名
  • 哪个网站可以做代练bt兔子磁力天堂
  • 论论坛坛网网站站建建设设百度网盘手机app下载安装
  • 网站建设 php 企业网站小程序生成平台系统
  • 沈阳三好街网站建设今天新闻头条
  • 新媒体 网站建设 影视快速建站工具
  • 网站建设客户常问到的问题外链怎么打开
  • 外贸网站建设服务器百度登录首页
  • 巩义做网站的百度指数趋势
  • 政府网站建设教程口碑营销策略
  • 花生壳做网站是否稳定yandex搜索引擎
  • 做网站需要的注意事项百度关键词排名代做
  • 杨凌区住房和城乡建设局网站谷歌排名优化
  • 找外国女朋友的网站建设关键词优化设计
  • 做网站视频今日武汉最新消息
  • 公众号小程序是什么成都关键词优化排名
  • mysql 视频网站开发厦门百度关键词推广
  • 安徽省建设业协会网站网站如何优化排名