I am trying to find the average amount for the recent 4 months for each customer. Below is the sample data
The expected result should be as below. For VENDOR_INVOICE_NUMBER = 3, since the history is for only 2 months. Average should be for those 2 months.
I tried below logic and it is not working as expected. Need some suggesstions.
select vendor_invoice_number,
vendor_invoice_number_1 as vendor_invoice_number_date,
date,
amount_due,
avg(amount_due) over (order by to_date(date, 'dd-mon-yy') range between interval '4' month preceding and current row) as average
from test_915
order by date desc;

