根据导入的行获取排名

mum43rcc  于 2021-06-19  发布在  Mysql
关注(0)|答案(0)|浏览(170)

我需要以这样一种方式来完成这一点,当一个学生的类是被导入,并有一个相关的部分,它应该在其特定的排名。

DROP PROCEDURE IF EXISTS sp_test;
Delimiter $$
CREATE PROCEDURE sp_Student_Classs1() 
BEGIN
DECLARE done, doneX int default 0; 
DECLARE var_Student_Class_IDX, var_importedX, var_SectionX, var_form_idX, rnX int;  -- curX variables;
DECLARE var_Student varchar(255);   
DECLARE curA CURSOR FOR select Student_id from temp_Students;
DECLARE continue handler FOR NOT FOUND SET done = 1;
Open curA;

WHILE (done = 0) do   -- curA while
FETCH NEXT FROM curA INTO var_Student;
if done = 0 then      -- curA if
drop table if exists temp_table;
set @create_imported =  concat('create table temp_table (select pp.Student_Class_ID, usi.imported, usi.Section, usi.form_id,  pp.last_update_date, ''null'' as rn ', 
        ' from Student_Classs pp left join ui_sections usi on usi.Instance_ID = pp.Student_Class_ID where pp.Student_ID = ', var_Student, ' order by pp.last_update_date)' );
        select @create_imported;      PREPARE stmt_name FROM @create_imported;     EXECUTE Stmt_name;     DEALLOCATE prepare stmt_name;
        begin
        declare curX CURSOR FOR select Student_Class_ID, imported, Section, form_id, rn from temp_table;
        DECLARE continue handler FOR NOT FOUND SET doneX = 1;
        open CurX;
        set doneX = 0; set @rn = 1;
        while (doneX = 0) do
        FETCH NEXT FROM curX into var_Student_Class_IDX, var_importedX, var_SectionX, var_form_idX, rnX;
        if doneX = 0 then 
        if (var_imported in (select SectionID from temp_table)) or (var_Section_X in (select imported from temp_table)) 
        then
        set @insertX = concat('update temp_table set rn = ', @rn, ' where Student_Class_ID = ',var_Student_Class_IDX);  select @insertX;
         PREPARE stmt_name FROM @insertX;     EXECUTE Stmt_name;     DEALLOCATE prepare stmt_name;
          else 
          set @rn = @rn+1;
          set @insertX = concat('update temp_table set rn = ', @rn, ' where Student_Class_ID = ',var_Student_Class_IDX);  select @insertX;        
        end if;
        end if;
        end while;
        close CurX;
        end;

我可以得到前3行的第一个秩1,但之后它会像2,3一样增加,但应该保持在2,2,2。取决于什么我可以得到这个逻辑正确?
结果应该如下所示:

Section Imported    Student_Class   Student form    Rank
720      (null)      630              111     4       1
935       493        584              111     5       1
493       720        733              111     6       1
38         6         988              111     4       2
6       (null)       986              111     3       2
459       38         531              111     2       2

265       (null)     704               25     3       1
547       (null)     692               25     1       2

你能帮我做这个吗?

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题