How to update the result field based on criteria?

Asked

Viewed 322 times

4

I have the following table:

CREATE TABLE ALUNO (
  RA NUMBER(9),
  DISCIPLINA VARCHAR2(30),
  MEDIA NUMBER(3,1),
  CARGA_HORA NUMBER(2),
  FALTAS NUMBER(2),
  RESULTADO VARCHAR2(10)
);

And the following data:

INSERT INTO ALUNO VALUES (1,'SQL',7.5,80,20,'');
INSERT INTO ALUNO VALUES (2,'PLSQL',5.5,80,20,'');
INSERT INTO ALUNO VALUES (3,'MBD',7.5,80,40,'');

I have the following sql:

select ra,
   Disciplina,
   Media,
   Carga_Hora,
   Faltas,
   case when (Media >= 7) and (trunc((Faltas / Carga_Hora * 100)) <= 25) then 'APROVADO'
        when (Media between 5 and 6.9) and (trunc((Faltas / Carga_Hora * 100)) <= 25) then 'EXAME'
   else 'REPROVADO'  end Resultado

from student order by ra

I need to create a block to fill the result field:

  1. If the student had an average equal to or greater than or equal to 7.0 and his absences did not exceed 25% of the course workload the result will be: APROVADO.
  2. If the student got an average of less than 7.0 and his absences do not exceed 25% of the course workload the result will be: EXAME.
  3. For other cases the result will be: REPROVADO.
  • I tried to adjust your question to be more readable. If there is something wrong, do not hesitate to edit it.

1 answer

1

If the idea is to update the table with the results, just do a UPDATE with a CASE:

UPDATE ALUNO SET RESULTADO = (
    CASE
      WHEN MEDIA >= 7.0 AND FALTAS / CARGA_HORA <= 0.25 THEN 'APROVADO'
      WHEN FALTAS / CARGA_HORA <= 0.25 THEN 'EXAME'
      ELSE 'REPROVADO'
    END
  );

Demo at Sqlfiffle

  • The second WHEN seems incomplete according to the rule in the question: "mean less than 7.0 and its absences do not exceed 25%", right?

  • I made an adjustment to the question, added the ready sql query, now how to have a block to insert the data in the column?

  • @Zuul wasn’t incomplete, just wrong! :\

  • @itasouza Just add your condition CASE in a command UPDATE, as per my reply.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.