当前位置:网站首页>MySQL窗口函数 OVER()函数介绍
MySQL窗口函数 OVER()函数介绍
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','南宁');
一、描述
OVER()的意思就是所有的数据都在窗口中
二、实例
OVER() 意思是所有的数据都在窗口中
# 计算所有商品的平均价格
select *,avg(price) over () from product;
OVER()用于将当前行与一个聚合值进行比较
# 计算商品价格与平均价格之差
select *,price-avg(price) over () from product;
OVER()和COUNT()组合
# 计算所有商品数量
select *,count(id) over() from product;
一句SQL中使用两个窗口函数
# 在商品表的基础上,添加平均价格和总金额两列
select *,avg(price) over (),sum(price) over () from product;
窗口函数和where一起使用
# 计算type1类型和type2类型的平均价格
select
*, avg(price) over()
from product
where type in ('type1','type2');
在过滤条件中不能使用OVER()
# 查询所有商品中,价格高于平均价格的商品(报错)
select
*,
avg(price) over()
from product
where price > avg(price) over();
# 查询所有商品中,价格高于平均价格的商品
select
*,
avg(price) over()
from product
where price > (select avg(price) from product);
边栏推荐
- Optimal Power Flow (OPF) for High Voltage Direct Current (HVDC) (Matlab code implementation)
- TCP 可靠吗?为什么?
- 在 360 度绩效评估中应该问的 20 个问题
- GTK实现旋转加载动画
- 1、实例开启无锁表结构变更以后,在任务编排中通过“单实例SQL”节点进行的结构变更,是优先采用无锁表
- 人脸识别损失函数的汇总 | Pytorch版本实现
- 13、OOM模拟
- Why do I strongly recommend using smart async?
- JS basics--judgment
- Basic knowledge points in js - events
猜你喜欢

带你了解什么是 Web3.0

Small Tools (4) integrated Seata1.5.2 distributed transactions

【QT】Qt项目demo:数据在ui界面上显示,鼠标双击可弹窗显示具体信息

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

Introduction to spark learning - 1

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

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

I am doing open source in Didi

MATLAB | 一种简易的随机曼陀罗图形生成函数

ReentrantLock详解
随机推荐
How to start an NFT collection
WordPress 5.2.3 更新,升级出现请求超时的解决方法
Convex Optimization of Optimal Power Flow (OPF) in Microgrids and DC Grids (Matlab Code Implementation)
CS免杀姿势
ReentrantLock详解
Awesome!Coroutines are finally here!Thread is about to be in the past
DC-DC 2C (40W/30W) JD6606SX2 power back application
Why do I strongly recommend using smart async?
Windows 事件转发到 SQL 数据库
不可忽略!户外LED显示屏的特点及优势
请问下阿里云全托管flink能执行两条flink sql命令么?
window.open不显示favicon.icon
A new round of competition for speech recognition has started. Will natural dialogue be the next commanding height?
Reptile attention
leetcode: 899. Ordered Queue [Thinking Question]
带你了解什么是 Web3.0
[Deep Learning] Today's bug (August 2)
[Unity Getting Started Plan] Basic Concepts (8) - Tile Map TileMap 01
如何启动 NFT 集合
[Code Hoof Set Novice Village 600 Questions] Define a function as a macro