用于将最高GPA排名为最低的Pl / SQL过程

时间:2015-11-06 23:35:34

标签: sql plsql

所以我正在尝试编写一个程序,它将使用游标排名最高的GPA = 1等等。我被困的地方是

declare
 snum students.snum%type;
 sname students.sname%type;
 GPA students.GPA%type;
begin 
 for Rec in 
   (select snum,sname,GPA 
      from students
      order by GPA desc)loop
  dbms_output.put_line(rec);
  end loop;
end;

我被困在循环中(现在它只是一个占位符)。我不确定设置每个GPA等级的正确方法。

1 个答案:

答案 0 :(得分:2)

oracle中有RANK功能。 See more here.

示例:

declare
 v_snum students.snum%type;    --Avoid to use "variable name" as same as "column name"
 v_sname students.sname%type;
 v_GPA students.GPA%type;
begin 
 for rec in 
   (select snum,
           sname, 
           GPA, 
           RANK() OVER (ORDER BY GPA desc) as srank
      from students)
 loop
  dbms_output.put_line(rec.snum  || '|' || rec.sname || '|' || rec.GPA || '|' || rec.srank);
 end loop;
end;