当前位置:网站首页>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;
边栏推荐
- 1、实例开启无锁表结构变更以后,在任务编排中通过“单实例SQL”节点进行的结构变更,是优先采用无锁表
- A new round of competition for speech recognition has started. Will natural dialogue be the next commanding height?
- 使用 PowerShell 将 Windows 转发事件导入 SQL Server
- Yii2安装遇到Loading composer repositories with package information
- opencv 读取和写入路径有汉字的处理方法
- 想进阿里?先来搞懂一下分布式事务
- WordPress建站技术笔记
- 请问下,flink cdc监控oracle,我看源码是通过sid方式的,请问怎么改成service
- 世界顶级级架构师编写2580页DDD领域驱动设计笔记,属实有牌面
- 扩展欧几里得求逆元实例
猜你喜欢
How Navicat connects to MySQL on a remote server

spark入门学习-2

AI也有健忘症?英国41岁教授专访:解决灾难性遗忘

Introduction to the advantages of the new generation mesh network protocol T-Mesh wireless communication technology

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

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

【899. Ordered Queue】

出海季,互联网出海锦囊之本地化

如何选择合适的损失函数,请看......

MySQL性能优化_小表驱动大表
随机推荐
一文看懂推荐系统:召回02:Swing 模型,和itemCF很相似,区别在于计算相似度的方法不一样
【QT】Qt项目demo:数据在ui界面上显示,鼠标双击可弹窗显示具体信息
参与便有奖,《新程序员》杂志福利来袭!
mysql delete 执行报错:You can‘t specify target table ‘doctor_info‘ for update in FROM clause
Ruoyi Ruoyi framework @DataScope annotation use and some problems encountered
请问下阿里云全托管flink能执行两条flink sql命令么?
带你了解什么是 Web3.0
DAYU200 OpenHarmony标准系统HDMI全屏显示
unity用代码生成LightProbeGroup
Reptile attention
To participate in sweepstakes, incoming new programmers magazine welfare!
用友YonSuite与旺店通数据集成对接-技术篇2
【899. Ordered Queue】
甲方不让用开源【监控软件】?大不了我自己写一个
Detailed explanation of ReentrantReadWriteLock
AWS China SDN Connector
Neural networks, cool?
Go Go 简单的很,标准库之 fmt 包的一键入门
攻防世界----bug
Analysis of ffplay video playback principle