Hive系列文章

  1. Hive表的基本操作
  2. Hive中的集合数据类型
  3. Hive动态分区详解
  4. hive中orc格式表的数据导入
  5. Java通过jdbc连接hive
  6. 通过HiveServer2访问Hive
  7. SpringBoot连接Hive实现自助取数
  8. hive关联hbase表
  9. Hive udf 使用方法
  10. Hive基于UDF进行文本分词
  11. Hive窗口函数row number的用法
  12. 数据仓库之拉链表

关注公众号:大数据技术派,回复: 资料,领取1024G资料。

同比环比的计算

测试数据

  1. 1,2020-04-20,420
  2. 2,2020-04-04,800
  3. 3,2020-03-28,500
  4. 4,2020-03-13,100
  5. 5,2020-02-27,300
  6. 6,2020-01-07,450
  7. 7,2019-04-07,800
  8. 8,2019-03-15,1200
  9. 9,2019-02-17,200
  10. 10,2019-02-07,600
  11. 11,2019-01-13,300
  1. CREATE TABLE ods_saleorder (
  2. order_id int ,
  3. order_time date ,
  4. order_num int
  5. )ROW FORMAT DELIMITED
  6. FIELDS TERMINATED BY ','
  7. ;
  8. LOAD DATA LOCAL INPATH '/Users/liuwenqiang/workspace/hive/saleorder.txt' OVERWRITE INTO TABLE ods.ods_saleorder;

销售量的月年占比

关联实现

  1. select
  2. a.m_num,a.cmonth,b.y_num,b.cyear,round( m_num / y_num, 2 ) AS ratio
  3. from(
  4. select
  5. sum(order_num) as m_num,
  6. DATE_FORMAT(order_time,'yyyy-MM') as cmonth
  7. from
  8. ods_saleorder
  9. group by
  10. DATE_FORMAT(order_time,'yyyy-MM')
  11. ) a
  12. inner join
  13. (
  14. select
  15. sum(order_num) as y_num,
  16. DATE_FORMAT(order_time,'yyyy') as cyear
  17. from
  18. ods_saleorder
  19. group by
  20. DATE_FORMAT(order_time,'yyyy')
  21. ) b
  22. on
  23. substring(a.cmonth,1,4)=b.cyear
  24. ;

窗口实现

  1. SELECT
  2. order_month,
  3. num,
  4. total,
  5. round( num / total, 2 ) AS ratio
  6. FROM
  7. (
  8. select
  9. substr(order_time, 1, 7) as order_month,
  10. sum(order_num) over (partition by substr(order_time, 1, 7)) as num,
  11. sum(order_num) over (partition by substr( order_time, 1, 4 ) ) total,
  12. row_number() over (partition by substr(order_time, 1, 7)) as rk
  13. from ods_saleorder
  14. ) temp
  15. where rk = 1;

同比环比

与上年度数据对比称"同比",与上月数据对比称"环比"。

相关公式如下:

  1. 同比增长率计算公式
  2. (当年值-上年值)/上年值x100%
  3. 环比增长率计算公式
  4. (当月值-上月值)/上月值x100%

lead lag 的实现

这里我们就用环比做个例子,同比类似

  1. select
  2. now_month,
  3. now_num,
  4. last_num,
  5. round( (now_num-last_num) / last_num, 2 ) as ratio
  6. FROM(
  7. select
  8. now_month,
  9. now_num,
  10. lag( t1.now_num, 1) over (order by t1.now_month ) as last_num
  11. from
  12. (
  13. select
  14. substr(order_time, 1, 7) as now_month,
  15. sum(order_num) as now_num
  16. from ods_saleorder
  17. group by
  18. substr(order_time, 1, 7)
  19. ) t1
  20. ) t2;

我们看到有null 值,这里我们可以使用,lag的默认值做一次优化

  1. select
  2. now_month,
  3. now_num,
  4. last_num,
  5. -- 分母是0的话返回值是null
  6. nvl(round( (now_num-last_num) / last_num, 2 ),0)as ratio
  7. FROM(
  8. select
  9. now_month,
  10. now_num,
  11. lag( t1.now_num, 1,0) over (order by t1.now_month ) as last_num
  12. from
  13. (
  14. select
  15. substr(order_time, 1, 7) as now_month,
  16. sum(order_num) as now_num
  17. from ods_saleorder
  18. group by
  19. substr(order_time, 1, 7)
  20. ) t1
  21. ) t2;

其实到这里我们就处理完了,但是这样真的对吗,我们看到'2020-01' 的last_num 是800 也就是'2019-04',其实到这里我们就明白了,我们的数据是不连续的,所以我们这样计算是不行的,如果每个月都齐全,都有数据lag(num,12)就可以。

那就只能做自关联了,这样的话我们可以对时间做精准的限制

自关联的实现

  1. with a as (
  2. select
  3. now_month,
  4. now_num,
  5. substr(date(concat(now_month,'-','01')) - INTERVAL '1' month, 1, 7) as last_month
  6. from(
  7. select
  8. substr(order_time, 1, 7) as now_month,
  9. sum(order_num) as now_num
  10. from ods_saleorder
  11. group by
  12. substr(order_time, 1, 7)
  13. ) tmp
  14. )
  15. select
  16. a1.now_month,a1.now_num,a1.last_month,a2.now_num,
  17. nvl(round( (a1.now_num-a2.now_num) / a2.now_num, 2 ),0) as ratio
  18. from
  19. a a1
  20. inner join
  21. a a2
  22. on
  23. a1.last_month=a2.now_month
  24. ;

这里的时间计算INTERVAL 你也可以换成其他函数

  1. with a as (
  2. select
  3. now_month,
  4. now_num,
  5. substr(add_months(concat(now_month,'-','01'),-1), 1, 7) as last_month
  6. from(
  7. select
  8. substr(order_time, 1, 7) as now_month,
  9. sum(order_num) as now_num
  10. from ods_saleorder
  11. group by
  12. substr(order_time, 1, 7)
  13. ) tmp
  14. )
  15. select
  16. a1.now_month,a1.now_num,a1.last_month,nvl(a2.now_num,0),
  17. nvl(round( (a1.now_num-a2.now_num) / a2.now_num, 2 ),0) as ratio
  18. from
  19. a a1
  20. left join
  21. a a2
  22. on
  23. a1.last_month=a2.now_month
  24. ;

猜你喜欢

Hadoop3数据容错技术(纠删码)

Hadoop 数据迁移用法详解

Flink实时计算topN热榜

数仓建模分层理论

数仓建模方法论

Hive之同比环比的计算的更多相关文章

  1. 再谈Cognos利用FM模型来做同比环比

    很早之前已经讲过 <Cognos利用DMR模型开发同比环比>这篇文章里说的是不利用过滤器,而是采用 except (lastPeriods (-9000,[订单数据分析].[日期维度].[ ...

  2. cognos report同比环比以及默认为当前月分析

    现在的需求是按月份分析不同时期的余额数据,.(报表工具:cognos report:建模工具:FM) ------------------------------------------------- ...

  3. MySQL统计同比环比SQL

    大体思路: MySQL没有类似oracle方便的统计函数,只能靠自己去硬计算:通过时间字段直接增加年份.月份,然后通过left join关联时间字段去计算环比.同比公式即可 原始表结构: 求同比SQL ...

  4. Oracle分析函数/排名函数/位移函数/同比环比

    分析函数 作用:分析函数可以在数据中进行分组,然后计算基于组的某种统计值,并且每一组的每一行都可以返回一个统计值.统计函数:MAX(字段名).MIN(字段名).AVG(字段名).SUM(字段名).CO ...

  5. 【hive】关于用户留存率的计算

    首先用户留存率一般是面向新增用户的概念,是指某一天注册后的几天还是否活跃,是以每天为单位进行计算的.一般收到的需求都是一个时间段内的新增用户的几天留存 (1)找到这个时间段内的新增用户(也可能含有地区 ...

  6. 数据可视化之DAX篇(十二)掌握时间智能函数,同比环比各种比,轻松搞定!

    https://zhuanlan.zhihu.com/p/55841964 时间可以说是数据分析中最常用的独立变量,工作中也常常会遇到对时间数据的对比分析.假设要计算上年同期的销量,在PowerBI中 ...

  7. MDX 占比同比环比

    http://blog.csdn.net/hero_hegang/article/details/9072889

  8. 实现同比、环比计算的N种姿势

    在做数据分析时,我们会经常听到同比.环比同比的概念.各个企业和组织在发布统计数据时,通常喜欢用同比.环比来和之前的历史数据进行比较,用来说明数据的变化情况.例如,统计局公布2022年1月份CPI同比增 ...

  9. 同比 VS 环比

    同比(YoY=year on year):与历史同时期比较,例如2014年7月份与2013年7月份相比,叫同比 环比(MoM=month on month):是本期统计数据与上期比较,例如2014年7 ...

随机推荐

  1. Python之路 - Day4 - Python基础4 (新版)

    本节内容 迭代器&生成器 装饰器 Json & pickle 数据序列化 软件目录结构规范 作业:ATM项目开发 1.列表生成式,迭代器&生成器 列表生成式 孩子,我现在有个需 ...

  2. SpringMVC 解析(一)概览

    Spring MVC是Spring提供的构建Web应用程序的框架,该框架遵循了Servlet规范,负责接收并处理Servelt容器传递的请求,并将响应写回Response.Spring MVC以Dis ...

  3. js复制标题和链接

    问题 常常在写博客和作业时候,需要附上参考链接. 希望可以一键得到标题和链接. 解决方案 普通元素 可以使用findid然后复制 但是标题无法使用 <!DOCTYPE html> < ...

  4. Natasha 4.0 探索之路系列(一) 概况

    Natasha 简介 Natasha 是一个基于 Roslyn 的动态编译类库, 它以极简的 API 完成了动态编译的大部分功能, 使用它可以在程序运行时编译出新的程序集. Natasha 允许开发人 ...

  5. Natasha 4.0 探索之路系列(二) "域"与插件

    域与ALC 在 Natasha 发布之后有不少小伙伴跑过来问域相关的问题, 能不能兼容 AppDomain, 如何使用 AppDomain, 为什么 CoreAPI 阉割了 AppDomain 等一系 ...

  6. tarjan全家桶

    tarjan 全家桶 关于tarjan 它太强了 CCCOrz dfs树&low dfs树:在图上做不重复经过同一点的dfs,经过的边与点形成一棵树.于是图上所有点都被这棵树包含,一部分边被包 ...

  7. gin框架的热加载方法

    gin是用于实时重新加载Go Web应用程序的简单命令行实用程序.只需gin在您的应用程序目录中运行,您的网络应用程序将 gin作为代理提供.gin检测到更改后,将自动重新编译您的代码.您的应用在下次 ...

  8. gin中使用路由组

    package main import ( "github.com/gin-gonic/gin" ) func main() { router := gin.Default() / ...

  9. 安装python3.6,设为默认,yum不能用

    安装python3.6 1.安装依赖包 yum -y install wget sqlite-devel xz gcc automake zlib-devel openssl-devel epel-r ...

  10. Java高级语法之反射

    Java高级语法之反射 什么是反射 java.lang包提供java语言程序设计的基础类,在lang包下存在一个子包:reflect,与反射相关的APIs均在此处: 官方对reflect包的介绍如下: ...