Cube和Grouping 和Rollup

增强的聚合 Cube和Grouping 和Rollup
这几个分析函数通常用于OLAP中,不能累加,而且需要根据不同维度上钻和下钻的指标统计,比如,分小时、天、月的UV数。

GROUPING SETS
在一个GROUP BY查询中,根据不同的维度组合进行聚合,等价于将不同维度的GROUP BY结果集进行UNION ALL,
其中的GROUPING__ID,表示结果属于哪一个分组集合。

selectuser_type,sales,count(user_id) as pv,GROUPING__ID 
from order_detail
group by user_type,sales
GROUPING SETS(user_type,sales) 
ORDER BY GROUPING__ID;+------------+--------+-----+---------------+--+
| user_type  | sales  | pv  | grouping__id  |
+------------+--------+-----+---------------+--+
| old        | NULL   | 3   | 1             |
| new        | NULL   | 7   | 1             |
| NULL       | 6      | 1   | 2             |
| NULL       | 5      | 2   | 2             |
| NULL       | 3      | 2   | 2             |
| NULL       | 2      | 2   | 2             |
| NULL       | 1      | 3   | 2             |
+------------+--------+-----+---------------+--+selectuser_type,sales,count(user_id) as pv,GROUPING__ID 
from order_detail
group by user_type,sales
GROUPING SETS(user_type,sales,(user_type,sales)) 
ORDER BY GROUPING__ID;+------------+--------+-----+---------------+--+
| user_type  | sales  | pv  | grouping__id  |
+------------+--------+-----+---------------+--+
| old        | NULL   | 3   | 1             |
| new        | NULL   | 7   | 1             |
| NULL       | 1      | 3   | 2             |
| NULL       | 6      | 1   | 2             |
| NULL       | 5      | 2   | 2             |
| NULL       | 3      | 2   | 2             |
| NULL       | 2      | 2   | 2             |
| old        | 3      | 1   | 3             |
| old        | 2      | 1   | 3             |
| old        | 1      | 1   | 3             |
| new        | 6      | 1   | 3             |
| new        | 5      | 2   | 3             |
| new        | 3      | 1   | 3             |
| new        | 1      | 2   | 3             |
| new        | 2      | 1   | 3             |
+------------+--------+-----+---------------+--+

CUBE
根据GROUP BY的维度的所有组合进行聚合。

selectuser_type,sales,count(user_id) as pv,GROUPING__ID 
from order_detail
group by user_type,sales
WITH CUBE 
ORDER BY GROUPING__ID;+------------+--------+-----+---------------+--+
| user_type  | sales  | pv  | grouping__id  |
+------------+--------+-----+---------------+--+
| NULL       | NULL   | 10  | 0             |
| new        | NULL   | 7   | 1             |
| old        | NULL   | 3   | 1             |
| NULL       | 6      | 1   | 2             |
| NULL       | 5      | 2   | 2             |
| NULL       | 3      | 2   | 2             |
| NULL       | 2      | 2   | 2             |
| NULL       | 1      | 3   | 2             |
| old        | 3      | 1   | 3             |
| old        | 2      | 1   | 3             |
| old        | 1      | 1   | 3             |
| new        | 6      | 1   | 3             |
| new        | 5      | 2   | 3             |
| new        | 3      | 1   | 3             |
| new        | 2      | 1   | 3             |
| new        | 1      | 2   | 3             |
+------------+--------+-----+---------------+--+

ROLLUP
是CUBE的子集,以最左侧的维度为主,从该维度进行层级聚合。

selectuser_type,sales,count(user_id) as pv,GROUPING__ID 
from order_detail
group by user_type,sales
WITH ROLLUP 
ORDER BY GROUPING__ID;+------------+--------+-----+---------------+--+
| user_type  | sales  | pv  | grouping__id  |
+------------+--------+-----+---------------+--+
| NULL       | NULL   | 10  | 0             |
| old        | NULL   | 3   | 1             |
| new        | NULL   | 7   | 1             |
| old        | 3      | 1   | 3             |
| old        | 2      | 1   | 3             |
| old        | 1      | 1   | 3             |
| new        | 6      | 1   | 3             |
| new        | 5      | 2   | 3             |
| new        | 3      | 1   | 3             |
| new        | 2      | 1   | 3             |
| new        | 1      | 2   | 3             |
+------------+--------+-----+---------------+--+

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/508755.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

常见维度建模错误

需要避免的常见维度建模错误 错误 10: 在事实表中放入文本属性 要从数据仓库事实表中 挑出这些文本属性,并将它们放入维度表中。 错误 9: 限制使用冗长的描述符以节省空间 维度表从几何上看总是比事实表小很多。 错误 8: 将层次划分为多个维度 以用户看来最自然最 有…

2020-09-21

columns has 234 elements while hbase.columns.mapping has 92 elements (counting the key if implicit)) 根本原因: 对于4000个字符,hive Metastore中SERDE_PARAMS表中PARAM_VALUE字段的字符限制是此问题的根本原因。 此限制可防止Hive创建具有高列数…

2020-09-23

insert into table ads_user_action_convert_day select ‘2019-02-10’, uv.day_count, ua.order_count, cast(ua.order_count/uv.day_count as decimal(10,2)) visitor2order_convert_ratio, ua.payment_count, cast(ua.payment_count/ua.order_count as decimal(10,2)) orde…

2020-09-28

Mybatis中#{}和${} 1、#{}将传入的数据都当成一个字符串,会对自动传入的数据加一个引号(单引号?双引号?加了引号就对了)如: //传入的值是sex order by #{column} 解析后为 order by “sex” //将会出错 2…

PageUtil

Data NoArgsConstructor public class PageUtil implements Serializable {private static final long serialVersionUID 1L;/*** 总记录数*/private int totalCount;/*** 每页记录数*/private int pageSize;/*** 总页数*/private int totalPage;/*** 当前页数*/private int c…

springboot+事务,多张表的操作事务回滚

第一步,在springboot的启动类上开启事务,注解 EnableTransactionManagement 第二步:事务注解,回滚 Transactional(rollbackFor Exception.class) //Transactional和try catch捕获异常会让注解失效,所以: 第…

@EnableTransactionManagement

GetMapping("/saveData")ApiOperation("传感器数据上云-红外测温及气体")Transactional(rollbackFor Exception.class)public WebResult saveData(InfrareDataReqVo infrareDataReqVo){try {PigHeatEntity pigHeatEntitynew PigHeatEntity();pigHeatEntit…

根据经纬度显示地图轨迹

后端代码 public boolean saveCarGPSData(CarGPSReqVo carGPSReqVo) {Map<String,String> paramsnew HashMap<>(2);params.put("key",trsConfig.getCarGPS_key());params.put("coorType",carGPSReqVo.getCoorType());params.put("pageNu…

Linux vim 全选

全选&#xff08;高亮显示&#xff09;&#xff1a;按esc后&#xff0c;然后ggvG或者ggVG 全部复制&#xff1a;按esc后&#xff0c;然后ggyG 全部删除&#xff1a;按esc后&#xff0c;然后dG

两阶段聚合(局部聚合+全局聚合)

// 第一步&#xff0c;给RDD中的每个key都打上一个随机前缀。 JavaPairRDD<String, Long> randomPrefixRdd rdd.mapToPair(new PairFunction<Tuple2<Long,Long>, String, Long>() {private static final long serialVersionUID 1L;Overridepublic Tuple2&l…

java实现考勤机信息同步

/** * 更新传感器的用户信息 * * param sensorNumber * param attendanceEmployeeVos */ private synchronized void initializeAdd(String sensorNumber, List attendanceEmployeeVos) { if (attendanceEmployeeVos ! null && attendanceEmployeeVos.size() > 0) {…

Bug管理

Bug严重程度和优先级 bug两个重要属性就是优先级&#xff08;Priority&#xff09;和严重性(Severity)。通常Bug管理系统将严重性分为五个等级&#xff1a;Blocker、Critical、Major、Normal、Minor、Trivial。将优先级分为Immediate、Urgent、High、Normal、Low。 Bug的严重程…

处理魔法值

使用static final 定义常量或使用enum值 public static final String DANGERKEY_TRUE "1";if(DANGERKEY_TRUE.equals(dangerKey)){BigDecimal warningValue new BigDecimal(JedisUtils.get(WARNING_VALUE_KEY));model.addAttribute("isDanger", true);mo…

flink流处理

<?xml version"1.0" encoding"UTF-8"?> - - flink_warehouse com.kaikeba.flink 1.0-SNAPSHOT 4.0.0 flink_study - - cloudera https://repository.cloudera.com/artifactory/cloudera-repos/ - - org.apache.flink flink-streamin…

flink批处理

4.1 State 4.1.1 state概述 Apache Flink — Stateful Computations over Data Streams 回顾单词计数的例子 java /** 单词计数 */ public class WordCount { public static void main(String[] args) throws Exception { StreamExecutionEnvironment env StreamExecution…

utils

Service(“MarketingExpensesService”) public class MarketingExpensesServiceImpl extends ServiceImpl<MarketingExpensesMapper,MarketingExpensesPO> implements MarketingExpensesService { Resource private MetadataApi metadataApi; Override public PageUtil …

使用Notepad++打造称心的IDE: Python PHP Perl

Notepad简介 Notepad 不仅有语法高亮度显示&#xff0c;也有语法折叠功能&#xff0c;并且支持宏以及扩充基本功能的外挂模组。 官方网站下载安装 https://notepad-plus-plus.org/官方仓库下载各种版本 https://notepad-plus-plus.org/repository插件下载 https://sourceforg…

DOS实用命令

查看DOS支持的命令 打开cmd窗口&#xff0c;使用help命令&#xff0c;查看各种命令&#xff0c;以及命令的作用 常用命令使用说明 命令的使用帮助可以在命令后添加 /?参数 C:\Users\Administrator\Desktop>dir /? 显示目录中的文件和子目录列表。DIR [drive:][path][…

CMD命令之BAT脚本语法详解

批量处理用DOS命令来处理文件&#xff0c;命令格式为.bat echo、、call、pause、rem(小技巧&#xff1a;用::代替rem)是批处理文件最常用的几个命令&#xff0c;我们就从他们开始学起。 注 首先, 不是一个命令, 而是DOS 批处理的一个特殊标记符, 仅用于屏蔽命令行回显. 下面…