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;
答案 0 :(得分:0)
是否可以在没有排名的情况下对学生及其GPA进行排名 功能
是的,使用纯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 |