I have a table named result having column name, rollno, sub1, sub2, sub3, max1, max2
etc.
sub1, sub2, sub3
will store marks obtained. I want to find max of sub1, sub2, sub3
and store it in max1
and find second max of sub1, sub2, sub3
and store it in max2
.
Example
sub1 sub2 sub3 max1 max2
10 15 20 20 15
40 10 25 40 25
33 64 51 64 51
Can anyone tell me the sql code for this ?
UPDATE
Instead of storing the maximum number to max1
and second maximum to max2
, I want to divide maximum number and second maximum number by 2 and want to store it in average
column without storing it in max1
and max2
.
Means I don't want two extra column max1
and max2
to store maximum and second maximum, then add it and divide it by 2 then store it in average
. I want to do average direclty.
Please update the code.
Example
sub1 sub2 sub3 average
10 15 20 17.5 ( (Maximum + Second Maximum)/2 )
40 10 25 32.5
33 64 51 57.5
Consider the following
So to get the
max1
andmax2
from the columns you can usegreatest
function.This will give you something as
You can use this for update command as
To get the average of max1 and max2 and update as