Hi,
I need to create a view that calculates weighted average from another table.
The fields I am using from the other table are
GRANT_NUM Number
OPTS_GRNTD Number
OPTS_VESTED Number
FAIR_VALUE Number
I need to calculate the weighted average FAIR_VALUE for each GRANT_NUM or set of GRANT_NUM (when there are more than one). OPTS_GRNTD is acutally equal to the sum of OPTS_VESTED for each grant number (so I guess I could divided by SUM(OPTS_VESTED) instead of MAX(OPTS_GRNTD). I hope I communicated the requirement correctly.
My experience with SQL is limited and not quite sure if I have this correct. Here is an example with real numbers.
GRANT_NUM OPTS_GRNTED FAIR_VALUE OPTS_VESTED
123 1000 3.0 500
123 1000 4.0 300
123 1000 6.0 200
So my weighted average would be
((500 * 3.0) + (300 * 4.0) + (200 * 6.0)) / 1000
which would be 3.9.
Is my view correct? Will it give the results I am looking for? BTW, OPTS_GRNTD will never be zero so I do not have to worry about divide by zero.
CREATE or REPLACE VIEW CUS_WEIGHTED_FAIRVALUE AS
SELECT
cf.GRANT_NUM FK_Grant_Number,
((SUM(cf.OPTS_VESTED * cf.FAIR_VALUE))/MAX(cf.OPTS_GRNTD)) Weighted_Fair_Value
FROM
cus_fasb cf
GROUP BY
cf.GRANT_NUM
I need to create a view that calculates weighted average from another table.
The fields I am using from the other table are
GRANT_NUM Number
OPTS_GRNTD Number
OPTS_VESTED Number
FAIR_VALUE Number
I need to calculate the weighted average FAIR_VALUE for each GRANT_NUM or set of GRANT_NUM (when there are more than one). OPTS_GRNTD is acutally equal to the sum of OPTS_VESTED for each grant number (so I guess I could divided by SUM(OPTS_VESTED) instead of MAX(OPTS_GRNTD). I hope I communicated the requirement correctly.
My experience with SQL is limited and not quite sure if I have this correct. Here is an example with real numbers.
GRANT_NUM OPTS_GRNTED FAIR_VALUE OPTS_VESTED
123 1000 3.0 500
123 1000 4.0 300
123 1000 6.0 200
So my weighted average would be
((500 * 3.0) + (300 * 4.0) + (200 * 6.0)) / 1000
which would be 3.9.
Is my view correct? Will it give the results I am looking for? BTW, OPTS_GRNTD will never be zero so I do not have to worry about divide by zero.
CREATE or REPLACE VIEW CUS_WEIGHTED_FAIRVALUE AS
SELECT
cf.GRANT_NUM FK_Grant_Number,
((SUM(cf.OPTS_VESTED * cf.FAIR_VALUE))/MAX(cf.OPTS_GRNTD)) Weighted_Fair_Value
FROM
cus_fasb cf
GROUP BY
cf.GRANT_NUM