当前位置:网站首页>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);
边栏推荐
- 全新探险者以40万的产品击穿豪华SUV价格壁垒
- 深入浅出Flask PIN
- Fortinet产品导入AWS AMI操作文档
- 甲方不让用开源【监控软件】?大不了我自己写一个
- Common distributed theories (CAP, BASE) and consensus protocols (Gosssip, Raft)
- Tolstoy: There are only two misfortunes in life
- 聊聊这个SaaS领域爆火的话题
- CopyOnWriteArrayList details
- 为教育插上数字化的翅膀,网易云信发布「互联网+教育」整体解决方案
- 【QT】Qt项目demo:数据在ui界面上显示,鼠标双击可弹窗显示具体信息
猜你喜欢

【Unity入门计划】基本概念(7)-Input Manager&Input类

Windows 事件转发到 SQL 数据库

How to start an NFT collection

DAYU200 OpenHarmony标准系统HDMI全屏显示

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

使用Make/CMake编译ARM裸机程序(基于HT32F52352 Cortex-M0+)

技术干货|如何将 Pulsar 数据快速且无缝接入 Apache Doris

MATLAB gcf figure save image with black background/transparent background

为教育插上数字化的翅膀,网易云信发布「互联网+教育」整体解决方案

Yii2安装遇到Loading composer repositories with package information
随机推荐
mysql delete execution error: You can't specify target table 'doctor_info' for update in FROM clause
[QT] Qt project demo: data is displayed on the ui interface, double-click the mouse to display specific information in a pop-up window
为教育插上数字化的翅膀,网易云信发布「互联网+教育」整体解决方案
带你了解什么是 Web3.0
全新探险者以40万的产品击穿豪华SUV价格壁垒
JS basics--judgment
机器人开发--Universal Scene Description(USD)
How Navicat connects to MySQL on a remote server
MATLAB | 七夕节快到了,还不给朋友安排上这个咕呱小青蛙?
产品以及研发团队有使用专业的办公软件,如禅道、蓝湖等,他们应该如何使用 Tita 系统?
Small Tools (4) integrated Seata1.5.2 distributed transactions
Spark entry learning-2
Essentially a database data recovery 】 【 database cannot read data recovery case
袁小林:沃尔沃专注于出行的安全感,并且把它做到极致
甲方不让用开源【监控软件】?大不了我自己写一个
常见分布式理论(CAP、BASE)和一致性协议(Gosssip、Raft)
MarkDown常用代码片段和工具
Go Go 简单的很,标准库之 fmt 包的一键入门
GTK实现旋转加载动画
Yuan xiaolin: Volvo focus on travel security, and put it perfectly