MTD:例:20160918求20160901-20160918的amount的sum(累加)
YTD: 例:20160918求20160101-20160918的amount的sum(累加)
YTM: 例:20160918求2016年1-9月的amount的sum(累加)
解决方法:使用hive的开窗函数
#原始表(100条数据)
+------------------+-------------------+--+
| date_orig.ddate | date_orig.amount |
+------------------+-------------------+--+
| 20160918 | 106 |
| 20180707 | 225 |
| 20181003 | 126 |
| 20161222 | 178 |
| 20170607 | 198 |
| 20181105 | 280 |
| 20170706 | 260 |
| 20180817 | 291 |
| 20180206 | 104 |
| 20181128 | 267 |
| 20170313 | 154 |
| 20170313 | 191 |
| 20180921 | 108 |
| 20170926 | 106 |
| 20171116 | 131 |
| 20170816 | 161 |
| 20180514 | 143 |
| 20160216 | 138 |
| 20170317 | 194 |
| 20160609 | 255 |
| 20170202 | 213 |
| 20180505 | 173 |
| 20160302 | 238 |
| 20180328 | 218 |
| 20180221 | 176 |
| 20181222 | 116 |
| 20180104 | 139 |
| 20170802 | 297 |
| 20160723 | 206 |
| 20160825 | 283 |
| 20181203 | 217 |
| 20180530 | 252 |
| 20171120 | 262 |
| 20160129 | 163 |
| 20160628 | 269 |
| 20161011 | 179 |
| 20170820 | 185 |
| 20160913 | 262 |
| 20171130 | 190 |
| 20170821 | 172 |
| 20160908 | 267 |
| 20161120 | 250 |
| 20180901 | 260 |
| 20181124 | 110 |
| 20161012 | 184 |
| 20180525 | 177 |
| 20181218 | 221 |
| 20161202 | 120 |
| 20181009 | 260 |
| 20160821 | 278 |
| 20170330 | 121 |
| 20160429 | 288 |
| 20170430 | 250 |
| 20161127 | 198 |
| 20160801 | 150 |
| 20180906 | 266 |
| 20160321 | 298 |
| 20180813 | 111 |
| 20170914 | 147 |
| 20160610 | 270 |
| 20170512 | 169 |
| 20180325 | 221 |
| 20171215 | 295 |
| 20160329 | 184 |
| 20180507 | 110 |
| 20160518 | 223 |
| 20180711 | 191 |
| 20170529 | 188 |
| 20180106 | 133 |
| 20180529 | 149 |
| 20180913 | 114 |
| 20160413 | 120 |
| 20180507 | 179 |
| 20180426 | 233 |
| 20181204 | 155 |
| 20170915 | 284 |
| 20171006 | 255 |
| 20161103 | 132 |
| 20160105 | 154 |
| 20171025 | 226 |
| 20180726 | 108 |
| 20170217 | 266 |
| 20160917 | 204 |
| 20181205 | 277 |
| 20180212 | 221 |
| 20170413 | 262 |
| 20180729 | 154 |
| 20180311 | 290 |
| 20170105 | 193 |
| 20160907 | 212 |
| 20180909 | 192 |
| 20170501 | 272 |
| 20180421 | 197 |
| 20171204 | 217 |
| 20181105 | 223 |
| 20170730 | 240 |
| 20160727 | 279 |
| 20180924 | 197 |
| 20170224 | 222 |
| 20180902 | 293 |
+------------------+-------------------+--+
#hive sql语句
===========================================================================================
第一种:
select distinct ddate,amount,
substr(ddate,1,6) as mtd_month,
(sum(amount) over(partition by substr(ddate,1,6) order by ddate))as mtd,
(sum(amount) over(partition by substr(ddate,1,6) order by substr(ddate,1,6)))as ytm,
substr(ddate,1,4) as ytd_year,
(sum(amount) over(partition by substr(ddate,1,4) order by ddate))as ytd
from date_orig order by ddate;
============================================================================================
第二种:
select d.ddate,d.amount,
substr(d.ddate,1,6) as mtd_month,
(sum(d.amount) over(partition by substr(d.ddate,1,6) order by d.ddate))as mtd,
(sum(d.amount) over(partition by substr(d.ddate,1,6) order by substr(d.ddate,1,6)))as ytm,
substr(d.ddate,1,4) as ytd_year,
(sum(d.amount) over(partition by substr(d.ddate,1,4) order by d.ddate))as ytd
from
(select distinct ddate,amount from date_orig) d order by d.ddate;
============================================================================================
#得到的结果
+-----------+-----------+------------+-------+-------+-----------+-------+--+
| d.ddate | d.amount | mtd_month | mtd | ytm | ytd_year | ytd |
+-----------+-----------+------------+-------+-------+-----------+-------+--+
| 20160105 | 154 | 201601 | 154 | 317 | 2016 | 154 |
| 20160129 | 163 | 201601 | 317 | 317 | 2016 | 317 |
| 20160216 | 138 | 201602 | 138 | 138 | 2016 | 455 |
| 20160302 | 238 | 201603 | 238 | 720 | 2016 | 693 |
| 20160321 | 298 | 201603 | 536 | 720 | 2016 | 991 |
| 20160329 | 184 | 201603 | 720 | 720 | 2016 | 1175 |
| 20160413 | 120 | 201604 | 120 | 408 | 2016 | 1295 |
| 20160429 | 288 | 201604 | 408 | 408 | 2016 | 1583 |
| 20160518 | 223 | 201605 | 223 | 223 | 2016 | 1806 |
| 20160609 | 255 | 201606 | 255 | 794 | 2016 | 2061 |
| 20160610 | 270 | 201606 | 525 | 794 | 2016 | 2331 |
| 20160628 | 269 | 201606 | 794 | 794 | 2016 | 2600 |
| 20160723 | 206 | 201607 | 206 | 485 | 2016 | 2806 |
| 20160727 | 279 | 201607 | 485 | 485 | 2016 | 3085 |
| 20160801 | 150 | 201608 | 150 | 711 | 2016 | 3235 |
| 20160821 | 278 | 201608 | 428 | 711 | 2016 | 3513 |
| 20160825 | 283 | 201608 | 711 | 711 | 2016 | 3796 |
| 20160907 | 212 | 201609 | 212 | 1051 | 2016 | 4008 |
| 20160908 | 267 | 201609 | 479 | 1051 | 2016 | 4275 |
| 20160913 | 262 | 201609 | 741 | 1051 | 2016 | 4537 |
| 20160917 | 204 | 201609 | 945 | 1051 | 2016 | 4741 |
| 20160918 | 106 | 201609 | 1051 | 1051 | 2016 | 4847 |
| 20161011 | 179 | 201610 | 179 | 363 | 2016 | 5026 |
| 20161012 | 184 | 201610 | 363 | 363 | 2016 | 5210 |
| 20161103 | 132 | 201611 | 132 | 580 | 2016 | 5342 |
| 20161120 | 250 | 201611 | 382 | 580 | 2016 | 5592 |
| 20161127 | 198 | 201611 | 580 | 580 | 2016 | 5790 |
| 20161202 | 120 | 201612 | 120 | 298 | 2016 | 5910 |
| 20161222 | 178 | 201612 | 298 | 298 | 2016 | 6088 |
| 20170105 | 193 | 201701 | 193 | 193 | 2017 | 193 |
| 20170202 | 213 | 201702 | 213 | 701 | 2017 | 406 |
| 20170217 | 266 | 201702 | 479 | 701 | 2017 | 672 |
| 20170224 | 222 | 201702 | 701 | 701 | 2017 | 894 |
| 20170313 | 154 | 201703 | 345 | 660 | 2017 | 1239 |
| 20170313 | 191 | 201703 | 345 | 660 | 2017 | 1239 |
| 20170317 | 194 | 201703 | 539 | 660 | 2017 | 1433 |
| 20170330 | 121 | 201703 | 660 | 660 | 2017 | 1554 |
| 20170413 | 262 | 201704 | 262 | 512 | 2017 | 1816 |
| 20170430 | 250 | 201704 | 512 | 512 | 2017 | 2066 |
| 20170501 | 272 | 201705 | 272 | 629 | 2017 | 2338 |
| 20170512 | 169 | 201705 | 441 | 629 | 2017 | 2507 |
| 20170529 | 188 | 201705 | 629 | 629 | 2017 | 2695 |
| 20170607 | 198 | 201706 | 198 | 198 | 2017 | 2893 |
| 20170706 | 260 | 201707 | 260 | 500 | 2017 | 3153 |
| 20170730 | 240 | 201707 | 500 | 500 | 2017 | 3393 |
| 20170802 | 297 | 201708 | 297 | 815 | 2017 | 3690 |
| 20170816 | 161 | 201708 | 458 | 815 | 2017 | 3851 |
| 20170820 | 185 | 201708 | 643 | 815 | 2017 | 4036 |
| 20170821 | 172 | 201708 | 815 | 815 | 2017 | 4208 |
| 20170914 | 147 | 201709 | 147 | 537 | 2017 | 4355 |
| 20170915 | 284 | 201709 | 431 | 537 | 2017 | 4639 |
| 20170926 | 106 | 201709 | 537 | 537 | 2017 | 4745 |
| 20171006 | 255 | 201710 | 255 | 481 | 2017 | 5000 |
| 20171025 | 226 | 201710 | 481 | 481 | 2017 | 5226 |
| 20171116 | 131 | 201711 | 131 | 583 | 2017 | 5357 |
| 20171120 | 262 | 201711 | 393 | 583 | 2017 | 5619 |
| 20171130 | 190 | 201711 | 583 | 583 | 2017 | 5809 |
| 20171204 | 217 | 201712 | 217 | 512 | 2017 | 6026 |
| 20171215 | 295 | 201712 | 512 | 512 | 2017 | 6321 |
| 20180104 | 139 | 201801 | 139 | 272 | 2018 | 139 |
| 20180106 | 133 | 201801 | 272 | 272 | 2018 | 272 |
| 20180206 | 104 | 201802 | 104 | 501 | 2018 | 376 |
| 20180212 | 221 | 201802 | 325 | 501 | 2018 | 597 |
| 20180221 | 176 | 201802 | 501 | 501 | 2018 | 773 |
| 20180311 | 290 | 201803 | 290 | 729 | 2018 | 1063 |
| 20180325 | 221 | 201803 | 511 | 729 | 2018 | 1284 |
| 20180328 | 218 | 201803 | 729 | 729 | 2018 | 1502 |
| 20180421 | 197 | 201804 | 197 | 430 | 2018 | 1699 |
| 20180426 | 233 | 201804 | 430 | 430 | 2018 | 1932 |
| 20180505 | 173 | 201805 | 173 | 1183 | 2018 | 2105 |
| 20180507 | 179 | 201805 | 462 | 1183 | 2018 | 2394 |
| 20180507 | 110 | 201805 | 462 | 1183 | 2018 | 2394 |
| 20180514 | 143 | 201805 | 605 | 1183 | 2018 | 2537 |
| 20180525 | 177 | 201805 | 782 | 1183 | 2018 | 2714 |
| 20180529 | 149 | 201805 | 931 | 1183 | 2018 | 2863 |
| 20180530 | 252 | 201805 | 1183 | 1183 | 2018 | 3115 |
| 20180707 | 225 | 201807 | 225 | 678 | 2018 | 3340 |
| 20180711 | 191 | 201807 | 416 | 678 | 2018 | 3531 |
| 20180726 | 108 | 201807 | 524 | 678 | 2018 | 3639 |
| 20180729 | 154 | 201807 | 678 | 678 | 2018 | 3793 |
| 20180813 | 111 | 201808 | 111 | 402 | 2018 | 3904 |
| 20180817 | 291 | 201808 | 402 | 402 | 2018 | 4195 |
| 20180901 | 260 | 201809 | 260 | 1430 | 2018 | 4455 |
| 20180902 | 293 | 201809 | 553 | 1430 | 2018 | 4748 |
| 20180906 | 266 | 201809 | 819 | 1430 | 2018 | 5014 |
| 20180909 | 192 | 201809 | 1011 | 1430 | 2018 | 5206 |
| 20180913 | 114 | 201809 | 1125 | 1430 | 2018 | 5320 |
| 20180921 | 108 | 201809 | 1233 | 1430 | 2018 | 5428 |
| 20180924 | 197 | 201809 | 1430 | 1430 | 2018 | 5625 |
| 20181003 | 126 | 201810 | 126 | 386 | 2018 | 5751 |
| 20181009 | 260 | 201810 | 386 | 386 | 2018 | 6011 |
| 20181105 | 280 | 201811 | 503 | 880 | 2018 | 6514 |
| 20181105 | 223 | 201811 | 503 | 880 | 2018 | 6514 |
| 20181124 | 110 | 201811 | 613 | 880 | 2018 | 6624 |
| 20181128 | 267 | 201811 | 880 | 880 | 2018 | 6891 |
| 20181203 | 217 | 201812 | 217 | 986 | 2018 | 7108 |
| 20181204 | 155 | 201812 | 372 | 986 | 2018 | 7263 |
| 20181205 | 277 | 201812 | 649 | 986 | 2018 | 7540 |
| 20181218 | 221 | 201812 | 870 | 986 | 2018 | 7761 |
| 20181222 | 116 | 201812 | 986 | 986 | 2018 | 7877 |
+-----------+-----------+------------+-------+-------+-----------+-------+--+
// 测试用例如下
// 设置元数据服务
Delegate.getDelegate().setMetaColumnService(new IMetaColumnService() {
@Override
public List<String> queryMetaColumn(String dbName, String tableName) {
return Collections.emptyList();
@Override
public boolean clearMetaColumnCache() {
return true;
// 设置通知服务
Delegate.getDelegate().setNoticeService(s->{});
// 具有子查询的
sql
String hql = "select id,name from (select id from table_1 where id={p0}) t1 inner join (select name --this is name\n from table_2) t2";
// 获取id字段的血缘
LineageNode idNode = Delegate.getDelegate().getLineage(hql, "id");
// 获取name字段的血缘
LineageNode nameNode = Delegate.getDelegate().getLineage(hql, "name");
// 打印血缘
Utils.printLineage(idNode,nameNode);
(1)利用窗函数
实现
同比环比计算
计算每月的销量情况的场景
中
,我们可以窗口函数Lag计算获得上个月的销量数据:
LAG(value, offset, DEFAULT) OVER ()
这个函数的功能就是返回与当前行向前偏移n行的目标行的数值,如LAG(sum(price), 1) OVER () 即可以获得前一行的销量数据。
而月环比的计算公式为 (当月销量-上月销量)/...
1、
Hive
是基于Hadoop的一个数据仓库系统,在各大公司都有广泛的应用。美团数据仓库也是基于
Hive
搭建,每天执行近万次的
Hive
ETL计算流程,负责每天数百GB的数据存储和分析。
Hive
的稳定性和性能对我们的数据分析非常关键。在几次升级
Hive
的过程
中
,我们遇到了一些大大小小的问题。通过向社区的咨询和自己的努力,在解决这些问题的同时我们对
Hive
将
SQL
编译为MapReduce的过程有了比较深入的理解。对这一过程的理解不仅帮助我们解决了一些
Hive
的bug,也有利于我们优化
Hive
SQL
,提升我们对
Hive
的掌控力,同时有能力去定制一些需要的功能。详细讲解
SQL
编译为MapReduce之
javax.jdo.option.ConnectionURL
jdbc:my
sql
://localhost:3306/
hive
?createDatabaseIfNotExist=true
JDBC connect string for a JDBC metastore
javax.jdo.option.ConnectionDriverName
com.my
sql
.jdbc.Driver
Driver class name for a JDBC metastore
javax.jdo.option.ConnectionUserName
username to use against metastore database
javax.jdo.option.ConnectionPassword
hadoop
password to use against metastore database
5.安装
hive
和mysq完成后,将my
sql
的连接jar包拷贝到$
HIVE
_HOME/lib目录下
如果出现没有权限的问题,在my
sql
授权(在安装my
sql
的机器上执行)
my
sql
-uroot -p
#(执行下面的语句 *.*:所有库下的所有表 %:任何IP地址或主机都可以连接)
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'root' WITH GRANT OPTION;
FLUSH PRIVILEGES;
6. Jline包版本不一致的问题,需要拷贝
hive
的lib目录
中
jline.2.12.jar的jar包替换掉hadoop
中
的
/home/hadoop/app/hadoop-2.6.4/share/hadoop/yarn/lib/jline-0.9.94.jar
启动
hive
bin/
hive
----------------------------------------------------------------------------------------------------
Hive
几种使用方式:
1.
Hive
交互shell bin/
hive
2.
Hive
JDBC服务(参考java jdbc连接my
sql
)
3.
hive
启动为一个服务器,来对外提供服务
bin/
hive
server2
nohup bin/
hive
server2 1>/var/log/
hive
server.log 2>/var/log/
hive
server.err &
启动成功后,可以在别的节点上用beeline去连接
bin/beeline -u jdbc:
hive
2://mini1:10000 -n root
bin/beeline
! connect jdbc:
hive
2://mini1:10000
4.
Hive
命令
hive
-e ‘
sql
’
bin/
hive
-e 'select * from t_test'
Hive
数据类型指的是表
中
列的字段类型,整体分为两类:原生数据类型(primitive data type)和复杂数据类型(compliex data type)。将Row写入文件时,首先调用SerDe(默认LazySimpleSerDe)的Serializer将对象转换成字节序列,然后调用OutputFormat将数据写入HDFS文件
中
。
Hive
的默认存储路径是${
Hive
_HOME}/conf/
hive
-site.xml配置文件的
hive
.metastore.dir属性指定,默认值是。
# 按照 year 来分组,按照 month 来排序
# 结果:n 月的值是本年 1月到 n 月的累计值
sum(val) over(partition by year order by month)
通过 explain select ... 来查看语句解析,可以简单理解为,在每一次 order by 之后
商业情景
YTD
分析属于同比分析类,其特点在于对比汇总值,即从年初第一日值一直至今的值累加。作用在于分析企业
中
长期的经营绩效。在
YTD
日期同比
中
需要注意的是取等量日期单位作为参照物。举例而言,假设当前日期是2018年7月21日,其同比计算则应为去年7月1号至21号的销售值对比,而不是对比2018年7月整个月份的值。可视化
实现
折线图和区域图组合,折线图适用于呈现日期变化下不同年份的绩效走势,而区域图适...
同比:与去年或着以前年度比
环比:某一时期内相邻的两个等长时段相比,例:去年五月同去年四月相比,今年第三季度同今年第二季度相比
同比=用今年减去年再除以去年/(本期数-同期数)÷同期数×100%
环比=用本月减上月再除以上月/(本期数-上期数)/上期数×100%
可以按年初至今 (
YTD
) 将数据加载到 Financial Consolidation and Close。如果数据加载到 FCCS_
YTD
_Input 视图维成员而非 FCCS_Periodic 成员,则 Financial Consolidation and Close 将填充周期性成员,以使
YTD
金额与输入金额匹配。在第一个期间除外的所有期间,
YTD
输入金额将减去前一期间
YTD
金...
关于lib/asm-offsets.c:1:0: error: bad value (armv5) for -march= switch解决办法
ti的am335x starter kit平台sdk
中
编译uboot的时候,出现这个问题,一直没有找到问题的点,百度说的是编译的时候工具链没有指定,但是重新指定工具链和架构之后,还是会出现这个问题,arm-linux-gnuea
SELECT '
YTD
' AS CATEGORY_TIME
,@today AS CURRENT_DAY
,DATEADD(year, -1, @today) AS LAST_CURRENT_DAY -- 同期
,DATEADD(year
1.servlet类
下面为导出的servlet类,继承了HttpServlet,
实现
了service方法,关键代码为红色部分,excelBook = export(request);
是根据请求参数查询结果并的到Workbook的实体,response用两个set方法设置好之后,用getOutp...