I have a column where I store the values:
Nome | Turma | Nota1 | Nota2 | Nota3 | Nota4
How would I get the highest grade from the Note1, Note2, Note3, and Note4 fields? I understand that MAX () does this, but how could I apply to my need?
I have a column where I store the values:
Nome | Turma | Nota1 | Nota2 | Nota3 | Nota4
How would I get the highest grade from the Note1, Note2, Note3, and Note4 fields? I understand that MAX () does this, but how could I apply to my need?
You can use CASE WHEN
to solve your problem by performing the query as follows:
SELECT Nome, Turma,
CASE
WHEN Nota1 >= Nota2 AND Nota1 >= Nota3 THEN Nota1
WHEN Nota2 >= Nota1 AND Nota2 >= Nota3 THEN Nota2
WHEN Nota3 >= Nota1 AND Nota3 >= Nota2 THEN Nota3
ELSE Nota4
END AS MaiorNota
FROM *suaTabela*
You can also use the GREATEST command by passing the number of parameters to be returned the highest value, for example:
SELECT Nome, Turma, GREATEST(Nota1, Nota2, Nota3, Nota4) AS MaiorNota FROM *suaTabela*
I hope I have helped!
SELECT GREATEST(Nota1, Nota2, Nota3, Nota4) FROM tabela WHERE id=1