I have a dataset like this:
+---------+--------+--------+
| Student | Score1 | Score2 |
+---------+--------+--------+
| 1 | 23 | 40 |
| 2 | 12 | 10 |
| 3 | 54 | 90 |
+---------+--------+--------+
I want to calculate the average of 2 points in each row. Theoretically, it's pretty simple - simple (score1 + score2/2). However, I ran into problems if one of the NULL values needs to be widely used NVL. The function AVG()will take care of all this for me, but it is intended to average several lines. Is there a way to use AVG for values on a single line?
Update
This is what I have at the moment, which applies to all possibilities (as far as I know). However, I think there should be a cleaner way?
SELECT
T1.STUDENT,
T1.SCORE1,
T1.SCORE2,
(NVL(T1.SCORE1,0) + NVL(T1.SCORE2,0))/DECODE((NVL2(t1.SCORE1,1,0) + NVL2(t1.SCORE2,1,0)),0,NULL,(NVL2(t1.SCORE1,1,0) + NVL2(t1.SCORE2,1,0))) AS AVG_SCORE
FROM STUDENTS T1;
source
share