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

presto高级用法(grouping、grouping sets)

目录

准备工作:

在hive中建表

在presto中计算

分解式

按照城市分组 统计人数

按照性别分组 统计人数

​编辑

按照爱好分组 统计人数

​编辑

按照城市和性别分组 统计人数

按照城市和爱好分组 统计人数

按照性别和爱好分组 统计人数

按照城市和性别还有爱好分组 统计人数

统计人数

合并式

presto使用grouping

presto使用grouping sets

grouping作用例子展示

高级用法: cube

rollup 用法


准备工作:

在hive中建表
drop database if exists db_test cascade;create database db_test;create table db_test.tb_student(name string,score   int,city    string,sex string,hobby string
)
row format delimited fields terminated by '\t';load data local inpath '/test/student.txt' into table db_test.tb_student;select * from db_test.tb_student;

student.txt数据

张三    10      北京    男      喝酒
李四    20      北京    男      抽烟
王五    30      北京    女      烫头
赵六    40      上海    男      抽烟
麻七    50      上海    女      烫头

在presto中计算

分解式
按照城市分组 统计人数
select city,count(1) as cnt from hive.db_test.tb_student group by city;

按照性别分组 统计人数
select hobby,count(1) as cnt from hive.db_test.tb_student group by hobby;
按照爱好分组 统计人数
select hobby,count(1) as cnt from hive.db_test.tb_student group by hobby;
按照城市和性别分组 统计人数
select city, sex, count(1) as cnt from hive.db_test.tb_student group by city, sex;

按照城市和爱好分组 统计人数
select city, hobby, count(1) as cnt from hive.db_test.tb_student group by city, hobby;

按照性别和爱好分组 统计人数
select sex, hobby, count(1) as cnt from hive.db_test.tb_student group by sex, hobby;

按照城市和性别还有爱好分组 统计人数
select city, sex, hobby, count(1) as cnt from hive.db_test.tb_student group by city, sex, hobby;

统计人数
select count(1) as cnt from hive.db_test.tb_student group by ();

合并式
with t1 as (select city, null as sex, null as hobby, count(1) as cnt, 1 as o from hive.db_test.tb_student group by cityunion allselect null as city, sex, null as hobby, count(1) as cnt, 2 as o from hive.db_test.tb_student group by sexunion allselect null, null, hobby,count(1) as cnt, 3 as o from hive.db_test.tb_student group by hobbyunion allselect city, sex, null, count(1) as cnt, 4 as o from hive.db_test.tb_student group by city, sexunion allselect city, null, hobby, count(1) as cnt, 5 as o from hive.db_test.tb_student group by city, hobbyunion allselect null, sex, hobby, count(1) as cnt, 6 as o from hive.db_test.tb_student group by sex, hobbyunion allselect city, sex, hobby, count(1) as cnt, 7 as o from hive.db_test.tb_student group by city, sex, hobbyunion allselect null, null, null, count(1) as cnt, 8 as o from hive.db_test.tb_student group by ()
)
select * from t1
order by o, city, sex, hobby
;

presto使用grouping

selectcity,sex,count(1) as cnt,grouping(city, sex) as g
from hive.db_test.tb_student
group by city, sex
;

presto使用grouping sets

selectcity,sex,hobby,count(1) as cnt,grouping(city, sex, hobby)
from hive.db_test.tb_student
group by grouping sets (city, sex, hobby)
;

selectcity,sex,hobby,count(1) as cnt,grouping(city, sex, hobby)
from hive.db_test.tb_student
group by grouping sets (city, sex, hobby, (city, sex), (city, hobby), (sex, hobby), (city, sex, hobby), ())
;

selectcity,sex,hobby,count(1) as cnt,casewhen grouping(city, sex, hobby)=3 then 1when grouping(city, sex, hobby)=5 then 2when grouping(city, sex, hobby)=6 then 3when grouping(city, sex, hobby)=1 then 4when grouping(city, sex, hobby)=2 then 5when grouping(city, sex, hobby)=4 then 6when grouping(city, sex, hobby)=0 then 7when grouping(city, sex, hobby)=7 then 8else 100end as o
from hive.db_test.tb_student
group by grouping sets (city, sex, hobby, (city, sex), (city, hobby), (sex, hobby), (city, sex, hobby), ())
order by o, city, sex, hobby
;

grouping作用例子展示

with t1 as (select '北京' as city, '男' as sexunion allselect '北京' as city, '男' as sexunion allselect '北京' as city, '女' as sexunion allselect '北京' as city, null as sex
)
selectcity,sex,count(1) as cnt
from t1
group by grouping sets (city, (city, sex))

问题:city=北京, sex=null, cnt=4city=北京, sex=null, cnt=1为什么 city 和 sex 的值一样, 但是结果不同?
原因:一个null 表示跟这一列没有关系另一个null 表示 这一列的值 为null, 根据 列值统计的结果怎么区分
解决方案:grouping(city, sex)0,0     两个都有关0,1     只跟city有关1,0     只跟sex有关1,1     都这两列都无关
with t1 as (select '北京' as city, '男' as sexunion allselect '北京' as city, '男' as sexunion allselect '北京' as city, '女' as sexunion allselect '北京' as city, null as sex
)
selectcity,sex,count(1) as cnt,grouping(city, sex) g
from t1
group by grouping sets (city, (city, sex))

selectcity,sex,hobby,count(1) as cnt,casewhen grouping(city, sex, hobby)=3 then 1when grouping(city, sex, hobby)=5 then 2when grouping(city, sex, hobby)=6 then 3when grouping(city, sex, hobby)=1 then 4when grouping(city, sex, hobby)=2 then 5when grouping(city, sex, hobby)=4 then 6when grouping(city, sex, hobby)=0 then 7when grouping(city, sex, hobby)=7 then 8else 100end as o
from hive.db_test.tb_student
group by grouping sets (city, sex, hobby, (city, sex), (city, hobby), (sex, hobby), (city, sex, hobby), ())
order by o, city, sex, hobby

高级用法: cube

selectcity,sex,hobby,count(1) as cnt,casewhen grouping(city, sex, hobby)=3 then 1when grouping(city, sex, hobby)=5 then 2when grouping(city, sex, hobby)=6 then 3when grouping(city, sex, hobby)=1 then 4when grouping(city, sex, hobby)=2 then 5when grouping(city, sex, hobby)=4 then 6when grouping(city, sex, hobby)=0 then 7when grouping(city, sex, hobby)=7 then 8else 100end as o
from hive.db_test.tb_student
group by cube(city, sex, hobby)
order by o, city, sex, hobby

rollup 用法

selectcity,sex,hobby,count(1) as cnt,casewhen grouping(city, sex, hobby)=3 then 1when grouping(city, sex, hobby)=5 then 2when grouping(city, sex, hobby)=6 then 3when grouping(city, sex, hobby)=1 then 4when grouping(city, sex, hobby)=2 then 5when grouping(city, sex, hobby)=4 then 6when grouping(city, sex, hobby)=0 then 7when grouping(city, sex, hobby)=7 then 8else 100end as o
from hive.db_test.tb_student
group by rollup(city, sex, hobby)
order by o, city, sex, hobby
;

总结:

presto时间函数:

date()类型 表示 年月日

timestamp类型表示 年月日时分秒

eg:timestamp('2024-08-18 22:13:10','%Y-%m-%d %H%i%s')

date_add(unit, value,timestamp) 

grouping sets()相当于一个集合 都能根据括号里的内容分组查询到相应的数据

grouping 根据8421码 0表示与该列有关系1表示无关 通过计算数值 查看与列之间分组的关系

cube(city, sex, hobby) 等价于 grouping sets (city, sex, hobby, (city, sex), (city, hobby), (sex, hobby), (city, sex, hobby), ())

rollup (city, sex, name) 等价于 grouping set((city, sex, name), (city, sex), city, ())

相关文章:

presto高级用法(grouping、grouping sets)

目录 准备工作: 在hive中建表 在presto中计算 分解式 按照城市分组 统计人数 按照性别分组 统计人数 ​编辑 按照爱好分组 统计人数 ​编辑 按照城市和性别分组 统计人数 按照城市和爱好分组 统计人数 按照性别和爱好分组 统计人数 按照城市和性别还有…...

二十五年后,Microsoft终于移除了FAT32的32GB分区限制——一个从草稿到现实的故事

二十五年后,Microsoft终于移除了FAT32的32GB分区限制——一个从草稿到现实的故事 你可能不知道,FAT32文件系统的32GB分区限制是怎么来的。这个限制其实是1994年Windows前开发者Dave Plummer无心插柳的结果,也是"草台班子"式开发的…...

Java二十三种设计模式-命令模式(18/23)

命令模式:将请求封装为对象的策略 概要 本文全面探讨了命令模式,从基础概念到实现细节,再到使用场景、优缺点分析,以及与其他设计模式的比较,并提供了最佳实践和替代方案,旨在帮助读者深入理解命令模式并…...

Kafka系列之:Dead Letter Queue死信队列DLQ

Kafka系列之:Dead Letter Queue死信队列DLQ 一、死信队列二、参数errors.tolerance三、创建死信队列主题四、在启用安全性的情况下使用死信队列更多内容请阅读博主这篇博客: Kafka系列之:Kafka Connect深入探讨 - 错误处理和死信队列一、死信队列 死信队列(DLQ)仅适用于接…...

Fragment学习笔记

静态加载 <fragment android:name"com.example.serviceapplication.fragment.TestFragment"android:layout_width"match_parent"android:layout_height"wrap_content"app:layout_constraintStart_toStartOf"parent"app:layout_cons…...

NGINX 基础参数与功能

章节 1 NGINX 的源码安装 2 NGINX 核心配置详解 3 NGINX 之 location 匹配优先级 4 NGINX 基础参数与功能 目录 1 实现 Nginx 账户认证功能 1.1 创建htpasswd 认证文件 1.2 创建数据目录 1.3 指定认证文件路径 1.4 测试效果 2 定义重定向错误日志 2.1 指定错误日志访问路…...

css设置元素居中显示

CSS中实现居中显示可以通过不同的属性来实现&#xff0c;取决于你是要水平居中还是垂直居中&#xff0c;或者两者都要。以下是一些常用的居中方法&#xff1a; 1.水平居中 - 行内元素或文本 .center-text {text-align: center; } 2.水平居中 - 块级元素 .center-block {mar…...

js判断一个任意值为空包括数组和对象

在JavaScript中&#xff0c;判断一个变量是否为空可以考虑以下几种情况&#xff1a; 如果变量可能是null或undefined&#xff0c;可以直接判断。 对于数组&#xff0c;如果想要判断数组为空&#xff08;长度为0&#xff09;&#xff0c;可以检查其length属性。 对于对象&…...

EmguCV学习笔记 VB.Net和C# 下的OpenCv开发

版权声明&#xff1a;本文为博主原创文章&#xff0c;转载请在显著位置标明本文出处以及作者网名&#xff0c;未经作者允许不得用于商业目的。 笔者的博客网址&#xff1a;https://blog.csdn.net/uruseibest 本教程将分为VB.Net和C#两个版本分别进行发布。 教程VB.net版本请…...

“TCP粘包”不是TCP的问题!

前言 写RPC用了Netty。涉及到粘包拆包问题。想复习一下。发现网上博客多是概念模糊不清。没有触及本质或者没有讲清楚。 遂决定自己写一篇 “TCP粘包”是谁的问题&#xff1f; 首先我们要明确TCP是面向字节流的协议。也就是说我们在应用层想使用TCP来传输数据时&#xff0c;…...

Electron项目依赖管理:最佳实践与常见错误

问题一 问题描述: 输入命令 pnpm add electron 后&#xff0c; electron 包在执行 postinstall 脚本时&#xff0c;尝试从网络上下载 Electron 二进制文件&#xff0c;但由于网络问题&#xff08;如连接超时或代理设置问题&#xff09;&#xff0c;导致下载失败。 λ pnpm a…...

华为数通路由交换HCIP/HCNP

2017-2022年软考高级网络规划设计师真题解析视频&#xff01;软考复习一定要多做历年真题&#xff01; 2022年软考网络规划设计师真题解析_哔哩哔哩_bilibili 2024年5月软考网络工程师真题解析合集&#xff0c;考后估分版【综合知识案例分析】 2024年5月软考网络工程师真题解…...

搜索面试题

1、目前怎么构建样本的&#xff1f;如果排序中第5个被点了&#xff0c;前面的作为负样本&#xff0c;后面的不要怎么样&#xff1b;为什么不好&#xff0c;为什么好。 点击作为负样本&#xff0c;曝光未点击作为负样本&#xff1b; 可以这样理解。您提到的排序中第5个被点的对…...

WPF学习(8) --Windows API函数的使用

一、API函数的介绍 1.FindWindow函数 [DllImport("user32.dll", CharSet CharSet.Auto)]public static extern IntPtr FindWindow(string lpClassName, string lpWindowName); 功能: FindWindow函数用于根据窗口的类名和窗口名称查找窗口的句柄&#xff08;IntPtr…...

Linux系统-用户账号文件

文章目录 文件一&#xff08;passwd&#xff09; 文件二&#xff08;shadow&#xff09; 加密密码部分 举例理解 文件三&#xff08;gshadow&#xff09; 文件四&#xff08;group&#xff09; 文件五&#xff08;skel&#xff09; 文件六&#xff08;login.defs&#…...

docker配置国内镜像加速

docker配置国内镜像加速 由于国内使用docker拉取镜像时&#xff0c;会经常出现连接超时的网络问题&#xff0c;所以配置Docker 加速来使用国内 的镜像加速服务&#xff0c;以提高拉取 Docker 镜像的速度。 1、备份docker配置文件 cp /etc/docker/daemon.json /etc/docker/da…...

C语言实现排序之堆排序算法

一、堆排序算法 基本思想 堆排序是一种比较有效的排序方法&#xff0c;其基本思想是&#xff1a; 构建最大堆&#xff1a;首先将待排序的数组构建成一个最大堆&#xff0c;即对于每个非叶子节点&#xff0c;它的值都大于或等于其子节点的值。排序&#xff1a;然后将堆顶元素…...

【STM32 Blue Pill编程】-外部中断配置及使用

外部中断配置及使用 文章目录 外部中断配置及使用1、中断介绍2、STM32中的中断3、硬件准备及接线4、GPIO配置5、代码实现在本文中,我们将介绍如何使用 STM32Cube IDE 中的 HAL 库配置和处理外部中断。 我们将通过一个带有按钮和 LED 的示例来演示这一点。 读完本文后,您将能够…...

MySQL 安装与配置教程:单机、主从复制与集群模式

目录 MySQL 简介MySQL 安装MySQL 基础配置MySQL 主从复制配置MySQL 集群配置总结 1. MySQL 简介 MySQL 是一个广泛使用的关系型数据库管理系统&#xff0c;具有高性能、高可靠性和易用性等特点。它支持多种部署模式&#xff0c;包括单机模式、主从复制模式&#xff08;用于高…...

JavaEE 的相关知识点(一)

一、过滤器 过滤器&#xff08;Filter&#xff09;是一个用于对请求和响应进行预处理的组件。过滤器可以在 Java Servlet 规范中使用&#xff0c;通常用于执行一些通用的任务 1、过滤器的作用 过滤器是一种javaEE规范中定义的一种技术&#xff0c;可以让请求达到目标servlet之…...

微软PowerBI考试 PL300-选择 Power BI 模型框架【附练习数据】

微软PowerBI考试 PL300-选择 Power BI 模型框架 20 多年来&#xff0c;Microsoft 持续对企业商业智能 (BI) 进行大量投资。 Azure Analysis Services (AAS) 和 SQL Server Analysis Services (SSAS) 基于无数企业使用的成熟的 BI 数据建模技术。 同样的技术也是 Power BI 数据…...

Python:操作 Excel 折叠

💖亲爱的技术爱好者们,热烈欢迎来到 Kant2048 的博客!我是 Thomas Kant,很开心能在CSDN上与你们相遇~💖 本博客的精华专栏: 【自动化测试】 【测试经验】 【人工智能】 【Python】 Python 操作 Excel 系列 读取单元格数据按行写入设置行高和列宽自动调整行高和列宽水平…...

【JVM】- 内存结构

引言 JVM&#xff1a;Java Virtual Machine 定义&#xff1a;Java虚拟机&#xff0c;Java二进制字节码的运行环境好处&#xff1a; 一次编写&#xff0c;到处运行自动内存管理&#xff0c;垃圾回收的功能数组下标越界检查&#xff08;会抛异常&#xff0c;不会覆盖到其他代码…...

【第二十一章 SDIO接口(SDIO)】

第二十一章 SDIO接口 目录 第二十一章 SDIO接口(SDIO) 1 SDIO 主要功能 2 SDIO 总线拓扑 3 SDIO 功能描述 3.1 SDIO 适配器 3.2 SDIOAHB 接口 4 卡功能描述 4.1 卡识别模式 4.2 卡复位 4.3 操作电压范围确认 4.4 卡识别过程 4.5 写数据块 4.6 读数据块 4.7 数据流…...

MVC 数据库

MVC 数据库 引言 在软件开发领域,Model-View-Controller(MVC)是一种流行的软件架构模式,它将应用程序分为三个核心组件:模型(Model)、视图(View)和控制器(Controller)。这种模式有助于提高代码的可维护性和可扩展性。本文将深入探讨MVC架构与数据库之间的关系,以…...

OkHttp 中实现断点续传 demo

在 OkHttp 中实现断点续传主要通过以下步骤完成&#xff0c;核心是利用 HTTP 协议的 Range 请求头指定下载范围&#xff1a; 实现原理 Range 请求头&#xff1a;向服务器请求文件的特定字节范围&#xff08;如 Range: bytes1024-&#xff09; 本地文件记录&#xff1a;保存已…...

跨链模式:多链互操作架构与性能扩展方案

跨链模式&#xff1a;多链互操作架构与性能扩展方案 ——构建下一代区块链互联网的技术基石 一、跨链架构的核心范式演进 1. 分层协议栈&#xff1a;模块化解耦设计 现代跨链系统采用分层协议栈实现灵活扩展&#xff08;H2Cross架构&#xff09;&#xff1a; 适配层&#xf…...

2025 后端自学UNIAPP【项目实战:旅游项目】6、我的收藏页面

代码框架视图 1、先添加一个获取收藏景点的列表请求 【在文件my_api.js文件中添加】 // 引入公共的请求封装 import http from ./my_http.js// 登录接口&#xff08;适配服务端返回 Token&#xff09; export const login async (code, avatar) > {const res await http…...

【AI学习】三、AI算法中的向量

在人工智能&#xff08;AI&#xff09;算法中&#xff0c;向量&#xff08;Vector&#xff09;是一种将现实世界中的数据&#xff08;如图像、文本、音频等&#xff09;转化为计算机可处理的数值型特征表示的工具。它是连接人类认知&#xff08;如语义、视觉特征&#xff09;与…...

Java多线程实现之Thread类深度解析

Java多线程实现之Thread类深度解析 一、多线程基础概念1.1 什么是线程1.2 多线程的优势1.3 Java多线程模型 二、Thread类的基本结构与构造函数2.1 Thread类的继承关系2.2 构造函数 三、创建和启动线程3.1 继承Thread类创建线程3.2 实现Runnable接口创建线程 四、Thread类的核心…...