Pl/SQL 在没有排名函数的情况下将最高 GPA 排名到最低的程序
Pl/SQL procedure to rank highest GPA to lowest without Rank Function
Pl/SQL procedure to rank highest GPA to lowest
我指的是上面的问题。是否可以在没有排名功能的情况下对学生及其 GPA 进行排名?
FETCH c1 INTO queryResult;
INSERT INTO GPA VALUES (queryResult.snum, queryResult.sname, rank,queryResult.OrderFrequency);
rank := rank + 1;
EXIT WHEN k = rank;
END LOOP;
CLOSE c1;
COMMIT;
END;
Is it possible to rank a student and their GPA without the rank
function?
是的,使用带左自连接、分组依据和计数的普通 SQL。
但预计会比使用 RANK
函数
的版本慢
现场演示:http://sqlfiddle.com/#!4/bde51/7
SELECT s1. snum,
s1.sname,
s1.gpa,
count( s2.gpa ) + 1 as rank
FROM students s1
LEFT JOIN students s2
ON s1.GPA < s2.GPA
GROUP BY s1. snum,
s1.sname,
s1.gpa
ORDER BY 4;
| SNUM | SNAME | GPA | RANK |
|------|-----------|-----|------|
| 6 | Student 6 | 4 | 1 |
| 4 | Student 4 | 3 | 2 |
| 3 | Student 3 | 2 | 3 |
| 1 | Student 1 | 2 | 3 |
| 2 | Student 2 | 1 | 5 |
| 5 | Student 5 | 1 | 5 |
select snum,
sname,
GPA,
RANK() OVER (ORDER BY GPA desc) as srank
from students
ORDER BY 4
| SNUM | SNAME | GPA | SRANK |
|------|-----------|-----|-------|
| 6 | Student 6 | 4 | 1 |
| 4 | Student 4 | 3 | 2 |
| 1 | Student 1 | 2 | 3 |
| 3 | Student 3 | 2 | 3 |
| 5 | Student 5 | 1 | 5 |
| 2 | Student 2 | 1 | 5 |
Pl/SQL procedure to rank highest GPA to lowest
我指的是上面的问题。是否可以在没有排名功能的情况下对学生及其 GPA 进行排名?
FETCH c1 INTO queryResult;
INSERT INTO GPA VALUES (queryResult.snum, queryResult.sname, rank,queryResult.OrderFrequency);
rank := rank + 1;
EXIT WHEN k = rank;
END LOOP;
CLOSE c1;
COMMIT;
END;
Is it possible to rank a student and their GPA without the rank function?
是的,使用带左自连接、分组依据和计数的普通 SQL。
但预计会比使用 RANK
函数
现场演示:http://sqlfiddle.com/#!4/bde51/7
SELECT s1. snum,
s1.sname,
s1.gpa,
count( s2.gpa ) + 1 as rank
FROM students s1
LEFT JOIN students s2
ON s1.GPA < s2.GPA
GROUP BY s1. snum,
s1.sname,
s1.gpa
ORDER BY 4;
| SNUM | SNAME | GPA | RANK |
|------|-----------|-----|------|
| 6 | Student 6 | 4 | 1 |
| 4 | Student 4 | 3 | 2 |
| 3 | Student 3 | 2 | 3 |
| 1 | Student 1 | 2 | 3 |
| 2 | Student 2 | 1 | 5 |
| 5 | Student 5 | 1 | 5 |
select snum,
sname,
GPA,
RANK() OVER (ORDER BY GPA desc) as srank
from students
ORDER BY 4
| SNUM | SNAME | GPA | SRANK |
|------|-----------|-----|-------|
| 6 | Student 6 | 4 | 1 |
| 4 | Student 4 | 3 | 2 |
| 1 | Student 1 | 2 | 3 |
| 3 | Student 3 | 2 | 3 |
| 5 | Student 5 | 1 | 5 |
| 2 | Student 2 | 1 | 5 |