当前位置:网站首页>MySQL窗口函数 PARTITION BY()函数介绍
MySQL窗口函数 PARTITION BY()函数介绍
2022-08-03 16:02:00 【飞Link】
前期数据准备
# 创建数据库
create database if not exists shopping charset utf8;
# 选择数据库
use shopping;
# 创建产品表
create table product
(
id int primary key,
name varchar(20),
price int,
type varchar(20),
address varchar(20)
);
# 插入产品数据
insert into shopping.product(id, name, price, type, address) values
(1,'商品1',200,'type1','北京'),
(2,'商品2',400,'type2','上海'),
(3,'商品3',600,'type3','深圳'),
(4,'商品4',800,'type1','南京'),
(5,'商品5',1000,'type2','成都'),
(6,'商品6',1200,'type3','武汉'),
(7,'商品7',1400,'type1','黑龙江'),
(8,'商品8',1600,'type2','黑河'),
(9,'商品9',1800,'type3','贵州'),
(10,'商品10',2000,'type1','南宁');
一、PARTITION BY与GROUP BY区别
一、函数类型
group by 是分组函数,partition by是分析函数
二、执行顺序
from > where > group by > having > order,而partition by应用在以上关键字之后,可以简单理解为就是在执行完select之后,在所得结果集之上进行partition by分组
二、查询结果
partition by 相比较于group by,能够在保留全部数据的基础上,只对其某些字段做分组排序,而group by则保留参与分组的字段和聚合函数的结果,类似excel中的透视表
二、PARTITION BY的基本用法
在OVER()中添加PARTITION BY
# 查询每种商品的id,name,同类型商品数量
select id,name,count(*) over (partition by type) from product;
PARTITION BY传入多列
# 查询每个城市每个类型价格最高的商品名称
select
name,
price,
type,
max(price) over (partition by address,type) as 'max_price'
from product;
边栏推荐
- Detailed ReentrantLock
- 高可用版 主数据库数据结构改变 备数据库会自动改变吗
- Windows 事件查看器记录到 MYSQL
- 新版本 MaxCompute 的SQL 中支持的 EXTRACT 函数有什么作用?
- 土耳其国防部:联合协调中心将对首艘乌克兰粮船进行安全检查
- 用户侧有什么办法可以自检hologres单表占用内存具体是元数据、计算、缓存的使用情况?
- 常见分布式理论(CAP、BASE)和一致性协议(Gosssip、Raft)
- STM32 GPIO LED and buzzer implementation [Day 4]
- STM32 GPIO LED和蜂鸣器实现【第四天】
- 【深度学习】今日bug(8月2)
猜你喜欢

MATLAB | 七夕节快到了,还不给朋友安排上这个咕呱小青蛙?

美国国防部更“青睐”光量子系统研究路线

用友YonSuite与旺店通数据集成对接-技术篇2

Small Tools (4) integrated Seata1.5.2 distributed transactions

Yuan xiaolin: Volvo focus on travel security, and put it perfectly

Research on power flow in DC microgrid based on Newton's method (Matlab code implementation)

深入浅出Flask PIN
![[Unity Getting Started Plan] Basic Concepts (8) - Tile Map TileMap 01](/img/8e/fcf79d150af4384c14a118fb209725.png)
[Unity Getting Started Plan] Basic Concepts (8) - Tile Map TileMap 01

AI+BI+可视化,Sugar BI架构深度剖析

STM32的HAL和LL库区别和性能对比
随机推荐
技术干货|如何将 Pulsar 数据快速且无缝接入 Apache Doris
ReentrantReadWriteLock详解
机器人开发--Universal Scene Description(USD)
使用 PowerShell 将 Windows 转发事件导入 SQL Server
[Deep Learning] Today's bug (August 2)
破解数字化转型困局,企业分析协同场景案例解析
如何启动 NFT 集合
How to start an NFT collection
opencv 读取和写入路径有汉字的处理方法
JS basics--judgment
window.open不显示favicon.icon
How to analyze the weekly activity rate?
When mobile applications go overseas, is your "network optimization" holding back?
产品以及研发团队有使用专业的办公软件,如禅道、蓝湖等,他们应该如何使用 Tita 系统?
How Navicat connects to MySQL on a remote server
Not to be ignored!Features and advantages of outdoor LED display
spark入门学习-1
Leetcode76. Minimal Covering Substring
移动应用出海,你的“网络优化”拖后腿了吗?
To add digital wings to education, NetEase Yunxin released the overall solution of "Internet + Education"