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);
- 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;
今天的開窗函數就學習到這里,后期我會更新 Clickhouse 的類似窗口函數。