MySQL开窗函数

Mysql 开窗函数在Mysql8.0+ 中可以得以使用,实在且好用。

  • row number() over
  • rank() over
  • dense rank()
  • ntile() 

我们先上测试数据,是不同姓名,不同课程的分数表;

复制代码

/*测试数据*/
CREATE TABLE `school_score` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` char(1) DEFAULT NULL,
    `course` char(10) DEFAULT NULL,
  `score`  int (2) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ;

INSERT INTO `test`.`school_score`(`id`, `name`,`course`,`score`) VALUES (1, 'A','Chinese',80);
INSERT INTO `test`.`school_score`(`id`, `name`,`course`,`score`) VALUES (2, 'B','Chinese',90);
INSERT INTO `test`.`school_score`(`id`, `name`,`course`,`score`) VALUES (3, 'C','Chinese',70);
INSERT INTO `test`.`school_score`(`id`, `name`,`course`,`score`) VALUES (4, 'A','Math',70);
INSERT INTO `test`.`school_score`(`id`, `name`,`course`,`score`) VALUES (5, 'B','Math',100);
INSERT INTO `test`.`school_score`(`id`, `name`,`course`,`score`) VALUES (6, 'C','Math',80);
INSERT INTO `test`.`school_score`(`id`, `name`,`course`,`score`) VALUES (7, 'A','English',90);
INSERT INTO `test`.`school_score`(`id`, `name`,`course`,`score`) VALUES (8, 'B','English',85);
INSERT INTO `test`.`school_score` (`id`, `name`,`course`,`score`) VALUES (9, 'C','English',99);

复制代码

 

  1. row number() over

复制代码

/*开窗函数和排名类函数结合,看每个课程的排名*/

SELECT
    `name`,
    `course`,
    `score`,
    row_number ( ) over ( PARTITION BY `course` ORDER BY score DESC ) AS score_rank 
FROM
    `test`.`school_score`;

复制代码

结果👇:

 

复制代码

    /*使用开窗函数计算每个课程分数最高的一个*/
    
SELECT
    * 
FROM
    ( SELECT `name`, `course`, `score`, row_number ( ) over ( PARTITION BY `course` ORDER BY score DESC ) AS score_rank FROM `test`.`school_score` ) AS a 
WHERE
    a.score_rank = 1;

复制代码

结果👇:

 

 

 

 

 

复制代码

    /*第二部分:开窗函数和SUM() ,AVG() 等聚合函数结合*/
    
SELECT
    `name`,
    `course`,
    `score`,
    SUM( score ) over ( PARTITION BY `course` ) AS course_score_total ,
    round(AVG(score) over (PARTITION BY `course`),2)  as  course_score_avg
FROM
    `test`.`school_score`;

复制代码

结果👇:

复制代码

/* SUM(score) over (PARTITION BY `course` ORDER BY score ASC)   如果执行这个语句,就是在每个
课程对分数进行累加*/

SELECT
    `name`,
    `course`,
    `score`,
    SUM(score) over (PARTITION BY `course` ORDER BY score ASC ) as course_score_total
FROM
`test`.`school_score`;

复制代码

 

 

 

思考🤔: 有order by ,按照排序连续累加;无order by ,计算partition by 后的和;over() 中没有partition by ,计算所有数据总和

同时,order by 的asc 和 desc 的排序不同,有order by 的结果也不一样。

 

2. row number() over , rank() over ,dense rank() 三者对比。

复制代码

create table students_score(
    id int(4)  auto_increment primary key,
    name varchar(50) not null, 
    score int(4) not null
    );
    
    insert into students_score(name,score) values
    ('A', 300),
    ('B', 240),
    ('C', 250), 
    ('D', 280), 
    ('E', 240), 
    ('F', 200);
    

复制代码

 

执行👇语句:

复制代码

SELECT
    `id`,
    `name`,
    rank ( ) over ( ORDER BY score DESC ) AS r,
    DENSE_RANK ( ) OVER ( ORDER BY score DESC ) AS dense_r,
    row_number ( ) OVER ( ORDER BY score DESC ) AS row_r 
FROM
    students_score;

复制代码

 

 👆 看图🤔区别,就可以知道三者的排名的区别了,如果我是校长,我希望可以按照 DENSE_RANK() 的排序,公平且可以激励着一代代莘莘学子。

 

3.ntile()分组

复制代码

SELECT
    `id`,
    `name`,
 score,
 ntile(3) over (order by score desc)  as n
FROM
    students_score;

复制代码

  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值