当前位置:网站首页>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;
边栏推荐
猜你喜欢
随机推荐
mysql delete execution error: You can't specify target table 'doctor_info' for update in FROM clause
为什么我强烈推荐使用智能化async?
我在滴滴做开源
【Unity入门计划】基本概念(8)-瓦片地图 TileMap 02
ffplay视频播放原理分析
出海季,互联网出海锦囊之本地化
TCP 可靠吗?为什么?
请问大家,MySQL全量怎么样可以提高性能呢?我这里瓶颈是在Source上,在不增加并行度的情况下,
ECCV 2022 | Relational Query-Based Temporal Action Detection Methods
Common distributed theories (CAP, BASE) and consensus protocols (Gosssip, Raft)
建造者模式/生成器模式
使用 PowerShell 将 Windows 转发事件导入 SQL Server
83. Remove Duplicates from Sorted List
土耳其国防部:联合协调中心将对首艘乌克兰粮船进行安全检查
基于DMS的数仓智能运维服务,知多少?
详谈RDMA技术原理和三种实现方式
【翻译】关于扩容一个百万级别用户系统的六个课程
Not to be ignored!Features and advantages of outdoor LED display
深入浅出Flask PIN
如何选择合适的损失函数,请看......