Here is the situation.
1)Say , I have a table fiscal_accrual
Create or replace table fiscal_accrual
(fiscal_accrual_id pls_integer,
Amount NUMBER,
fiscal_month DATE ) e.g like the first date of each month
);
2) I will want a procedure to return the average amount for 4 months based on particular month based on a month passed as a parameter.
Cursors may be used .
Calculation is done as per below: (July and August can never be a part of the resultset for calculation
a) If the passed parameter month is Feb,
then calculate the average by taking amount from 3 prior months and 1 succeeding month. e.i amount of Nov,Dec,Jan & March
b)if the passed parameter month is March,
then calculate the average by taking bonus_amt from 4 prior months e.i amount of Nov,Dec,Jan ,Feb
c)if the passed parameter month is April,
then calculate the average by taking amount from 4 successive months e.i bonus amt of may,Jun,Sept ,Oct
d)if the passed parameter month is May,
then calculate the average by taking amount from 1 prior month and 3 successive months e.i bonus amt of April,Jun,Sept ,Oct
e)if the passed parameter month is June,
then calculate the average by taking amount from 2 prior month and 2 successive months e.i bonus amt of April,May,Sep ,Oct
f)if the passed parameter month is July,
then calculate the average by taking amount from 2 prior month and
And likewise, but never include july and august .
d) basically taking average of 4 months based on above conditions.If no rows are returned, just get 4 months in any order.
No hard coding is allowed. Should be dynamic