SQL266. 考试分数(一)
描述
牛客每次考试完,都会有一个成绩表(grade),如下:
id | job | score |
1 | C++ | 11001 |
2 | C++ | 10000 |
3 | C++ | 9000 |
4 | Java | 12000 |
5 | Java | 13000 |
6 | JS | 12000 |
7 | JS | 11000 |
8 | JS | 9999 |
第1行表示用户id为1的用户选择了C++岗位并且考了11001分
。。。
第8行表示用户id为8的用户选择了JS岗位并且考了9999分
请你写一个sql语句查询各个岗位分数的平均数,并且按照分数降序排序,结果保留小数点后面3位(3位之后四舍五入):
job | avg |
Java | 12500.000 |
JS | 10999.667 |
C++ | 10000.333 |
(注意: sqlite 1/2得到的不是0.5,得到的是0,只有1*1.0/2才会得到0.5,sqlite四舍五入的函数为round)
示例1
输入:
drop table if exists grade; CREATE TABLE grade( `id` int(4) NOT NULL, `job` varchar(32) NOT NULL, `score` int(10) NOT NULL, PRIMARY KEY (`id`)); INSERT INTO grade VALUES (1,'C++',11001), (2,'C++',10000), (3,'C++',9000), (4,'Java',12000), (5,'Java',13000), (6,'JS',12000), (7,'JS',11000), (8,'JS',9999);
输出:
Java|12500.000 JS|10999.667 C++|10000.333
Sqlite 解法, 执行用时: 9ms, 内存消耗: 3496KB, 提交时间: 2021-08-09
select job,Round(AVG(score),3) as avg from grade group by job order by avg desc
Sqlite 解法, 执行用时: 10ms, 内存消耗: 3320KB, 提交时间: 2020-12-30
select job, round(avg(score),3) from grade group by job order by avg(score) desc
Sqlite 解法, 执行用时: 10ms, 内存消耗: 3368KB, 提交时间: 2021-09-15
select job,round(avg(score),3) as avg from grade group by job order by avg desc
Sqlite 解法, 执行用时: 10ms, 内存消耗: 3368KB, 提交时间: 2021-09-05
select job,round(avg(score), 3) avg from grade group by job order by avg desc;
Sqlite 解法, 执行用时: 10ms, 内存消耗: 3372KB, 提交时间: 2021-09-06
select job, ROUND(AVG(score), 3) as avg from grade group by job order by avg desc;