Hive实现按照指定格式输出每七天的消费平均数

数据准备

2018/6/1,10
2018/6/2,11
2018/6/3,11
2018/6/4,12
2018/6/5,14
2018/6/6,15
2018/6/7,13
2018/6/8,37
2018/6/9,18
2018/6/10,19
2018/6/11,10
2018/6/12,11
2018/6/13,11
2018/6/14,12
2018/6/15,14
2018/6/16,15
2018/6/17,13
2018/6/18,17
2018/6/19,18
2018/6/20,19
2018/6/21,20
2018/6/22,21
2018/6/23,21
2018/6/24,22
2018/6/25,24
2018/6/26,25
2018/6/27,23
2018/6/28,27
2018/6/29,28
2018/6/30,29
2018/7/1,40
2018/7/2,41
2018/7/3,41
2018/7/4,42
2018/7/5,44
2018/7/6,45
2018/7/7,43
2018/7/8,47
2018/7/9,48
2018/7/10,49
2018/7/11,50
2018/7/12,51
2018/7/13,51
2018/7/14,52
2018/7/15,54
2018/7/16,55
2018/7/17,53
2018/7/18,57
2018/7/19,58
2018/7/20,59
2018/7/21,30
2018/7/22,31
2018/7/23,31
2018/7/24,32
2018/7/25,34
2018/7/26,35
2018/7/27,33
2018/7/28,37
2018/7/29,38
2018/7/30,39
2018/7/31,70
2018/8/1,71
2018/8/2,71
2018/8/3,72
2018/8/4,74
2018/8/5,75
2018/8/6,73
2018/8/7,77
2018/8/8,78
2018/8/9,79
2018/8/10,80
2018/8/11,81
2018/8/12,81

创建表

create table f
(
date_time string,
cost string
)
ROW FORMAT DELIMITED FIELDS TERMINATED BY ',';

使用本地加载数据

  load data local inpat '文件所在的绝对路径' into table f;

查看内容

需求: 求每七天的消费平均数 ,要求输出格式如下:

第一步,先将日期分割

select split(date_time,'/') from f;

第二步,将日期使用‘-’ 进行连接,使用到 concat_ws() 函数

select concat_ws('-',split(date_time,'/')) from f;

第三步,我们要实现这个要求,主要思想就是怎么去实现统一分组,以及日期怎么去变化,先将每个日期进行减去第一天的日期。 datediff()

select datediff(concat_ws('-',split(date_time,'/')),'2018-6-1') from f;

第四步,在上面的基础之上进行除以7,因为hive有小数点,我们 floor再向下取整,这样我们发现,日期现在变的一致了。

select floor(datediff(concat_ws('-',split(date_time,'/')),'2018-6-1')/7)*7,cost from f;

第五步,现在需要思考的就是,怎么将其转化位日期,而且还要有分组之前的现象出现,我们使用第一天的日期加上当前的日期*7就是一样的了。

select date_add('2018-6-1',cast(floor(datediff(concat_ws('-',split(date_time,'/')),'2018-6-1')/7)*7 as int)),cost from f;

第六步,我们距离要求输出越来越近,现在需要考虑的就是怎么获取7天后的日期,以及怎么输出指定格式。很简单,后面7天的日期在当前日期前面加6就是的了,在使用函数 concat() 进行拼接,当然也可以使用concat_ws() 因为我这里就两个量进行拼接,可以用concat()   

select concat(date_add('2018-6-1',cast(floor(datediff(concat_ws('-',split(date_time,'/')),'2018-6-1')/7)*7 as int)),'~',date_add('2018-6-1',cast(floor(datediff(concat_ws('-',split(date_time,'/')),'2018-6-1')/7)*7+6 as int))) as dtime,cost from f;

第七步,到这里应该就很熟悉了,我们现在就是要做的就是对这个结果进行group by分组,以及avg()求平均值了

select w.dtime,avg(w.cost) from (select concat(date_add('2018-6-1',cast(floor(datediff(concat_ws('-',split(date_time,'/')),'2018-6-1')/7)*7 as int)),'~', date_add('2018-6-1',cast(floor(datediff(concat_ws('-',split(date_time,'/')),'2018-6-1')/7)*7+6 as int))) as dtime,cost from f) w group by w.dtime;select w.dtime,avg(w.cost) from (select concat(date_add('2018-6-1',cast(floor(datediff(concat_ws('-',split(date_time,'/')),'2018-6-1')/7)*

select w.dtime,avg(w.cost) from (select concat(date_add('2018-6-1',cast(floor(datediff(concat_ws('-',split(date_time,'/')),'2018-6-1')/7)*7 as int)),'~', date_add('2018-6-1',cast(floor(datediff(concat_ws('-',split(date_time,'/')),'2018-6-1')/7)*7+6 as int))) as dtime,cost from f) w group by w.dtime;

第八步,我们将小数点保留至后两位,使用函数round()

select w.dtime,round(avg(w.cost),2) from (select concat(date_add('2018-6-1',cast(floor(datediff(concat_ws('-',split(date_time,'/')),'2018-6-1')/7)*7 as int)),'~', date_add('2018-6-1',cast(floor(datediff(concat_ws('-',split(date_time,'/')),'2018-6-1')/7)*7+6 as int))) as dtime,cost from f) w group by w.dtime;

最终结果:

Week08_day01 (Hive实现按照指定格式输出每七天的消费平均数)的更多相关文章

  1. javascript两行代码按指定格式输出日期时间

    javascript两行代码按指定格式输出日期时间,具体看代码: function date2str(x,y) { var z ={y:x.getFullYear(),M:x.getMonth()+1 ...

  2. js 转换时间戳为时间格式并且按指定格式输出

    /** * 时间戳转换为日期 */ function convertTimestamp(timestamp){ // 时间戳转换为日期 var d = new Date(timestamp); // ...

  3. java按照指定格式输出系统时间

    public class TimeFour { public static void main(String[] args) throws ParseException{ TimeFour four ...

  4. java按照指定格式输出系统时间使用SimpleDateFormat方法

    public class TimeThree { public static void main(String[] args) { SimpleDateFormat d = new SimpleDat ...

  5. python完成加密参数sign计算并输出指定格式的字符串

    加密规则: 1.固定加密字符串+字符串组合(key/value的形式,并通过aissc码排序), 2.通过sha1算法对排序后的字符串进行加密, 3.最终输出需要的参数sign 4.完成请求参数数据的 ...

  6. C 格式输出

    1 一般格式    printf(格式控制,输出表列)    例如:printf("i=%d,ch=%c\n",i,ch);    说明:    (1) “格式控制”是用双撇号括起 ...

  7. 《The Linux Command Line》 读书笔记03 ls命令与长格式输出解释 文件权限

    ls命令与长格式输出解释 文件权限 ls命令 ls 命令用于列出目录内容,不带参数时列出当前工作目录的内容,也可以指定目标目录(可以指定多个),列出目标目录下的内容. ls命令的参数 ls -l 长格 ...

  8. python基础入门--input标签、变量、数字类型、列表、字符串、字典、索引值、bool值、占位符格式输出

    # 在python3 中: # nian=input('>>:') #请输入什么类型的值,都成字符串类型# print(type(nian)) # a = 2**64# print(typ ...

  9. WPF绑定文本时使用指定格式文本

    原文:WPF绑定文本时使用指定格式文本 Text="{Binding PlayletModel.characters,StringFormat=Cast : {0}}" Strin ...

随机推荐

  1. 【计算机视觉】detection/region/object proposal 方法综述文章

    目录(?)[-] Papers 大纲 各种OP方法的回顾 Grouping proposal methods Window scoring proposal methods Aliternate pr ...

  2. nginx目录学习

    目录 一. Nginx 基础知识 二. Nginx 安装及调试 三. Nginx Rewrite 四. Nginx Redirect 五. Nginx 目录自动加斜线: 六. Nginx Locati ...

  3. golang 切片和map查询比较

    package main import ( "fmt" "time" ) var testTimeSlice = []string{"aa" ...

  4. Kali中安装VMwaretools

    VMware Workstation 中 虚拟机选项,安装VMware Tools 选项. 在虚拟机中,打开VMware Tools,将 VMwareTools-10.1.15-6627299.tar ...

  5. [转帖]详解oracle数据库唯一主键SYS_GUID()

    详解oracle数据库唯一主键SYS_GUID() https://www.toutiao.com/i6728736163407856139/ 其实 需要注意 这里满不能截取 因为截取了 就不一定唯一 ...

  6. 把cgrep mgrep集成到bashrc

    https://android.googlesource.com/platform/build/+/android-4.4.3_r1/envsetup.sh 在~/.bashrc里面增加: #Andr ...

  7. 斜率优化dp学习笔记 洛谷P3915[HNOI2008]玩具装箱toy

    本文为原创??? 作者写这篇文章的时候刚刚初一毕业…… 如有错误请各位大佬指正 从例题入手 洛谷P3915[HNOI2008]玩具装箱toy Step0:读题 Q:暴力? 如果您学习过dp 不难推出d ...

  8. s5p6818 从SD卡启动程序(制作SD启动卡)

    背景: 最近在学习uboot,其中有一步很重要的任务就是需要实现uboot 的验证,没有办法验证uboot是不是自己做的,那么整个开发就会收到阻碍.另外,从公司现在开发的板子来看,uboot从sd卡启 ...

  9. java——值传递和引用传递

    值传递 在方法被调用时,实参通过形参把它的内容副本传入方法内部,此时形参接收到的内容是实参值的一个拷贝,因此在方法内对形参的任何操作,都仅仅是对这个副本的操作,不影响原始值的内容. 先来看个例子: p ...

  10. (三十一)web 开发基础项目

    1. 编写index.jsp <%@ page language="java" contentType="text/html; charset=UTF-8" ...