CREATE TABLE `score_table` ( `stu_id` varchar(20) DEFAULT NULL, `subject_name` varchar(20) DEFAULT NULL, `score` int DEFAULT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ;表中全部记录如下:
+--------+--------------+-------+ | stu_id | subject_name | score | +--------+--------------+-------+ | 1004 | 语文 | 90 | | 1004 | 数学 | 82 | | 1004 | 英语 | 88 | | 1009 | 语文 | 91 | | 1009 | 数学 | 92 | | 1009 | 英语 | 78 | | 1010 | 语文 | 71 | | 1010 | 数学 | 82 | | 1006 | 语文 | 61 | | 1012 | 数学 | 80 | | 1012 | 语文 | 80 | +--------+--------------+-------+希望得到如下结果:按照subject_name分组后按照score升序排序,percent=(当前记录在分区内的序号-1)除以(总行数)。比如stu_id=1012,在数学分区内序号是1,数学分区内共4条记录,所以percent=(1-1)/(4-1)=0%。对于重复值,取重复值第一行的序号,比如stu_id=1004与stu_id=1010的数学score都是82,第一行的序号是2,所以percent=(2-1)/(4-1)=33.33%。
+--------+--------------+-------+---------+ | stu_id | subject_name | score | percent | +--------+--------------+-------+---------+ | 1012 | 数学 | 80 | 0% | | 1004 | 数学 | 82 | 33.33% | | 1010 | 数学 | 82 | 33.33% | | 1009 | 数学 | 92 | 100% | | 1009 | 英语 | 78 | 0% | | 1004 | 英语 | 88 | 100% | | 1006 | 语文 | 61 | 0% | | 1010 | 语文 | 71 | 25% | | 1012 | 语文 | 80 | 50% | | 1004 | 语文 | 90 | 75% | | 1009 | 语文 | 91 | 100% | +--------+--------------+-------+---------+以下SQL符合预期的是()?
SELECT stu_id , subject_name ,score , concat(round( quantile_rank() over(partition by subject_name order by score),4)*100,'%') AS percent FROM score_table;
SELECT stu_id , subject_name ,score , concat(round( percent_rank() over(partition by subject_name order by score),4)*100,'%') AS percent FROM score_table;
SELECT stu_id , subject_name ,score , concat(round( percentile_rank() over(partition by subject_name order by score),4)*100,'%') AS percent FROM score_table;
其他选项均不符合预期