You can fill the table with the missing rows using the generate_series () function, so the window function will return the correct data. You can select a report period with start and end dates in generate_series () :
select stock, date, price, max(price) over (partition by stock order by date rows 90 preceding) from ( select d::date as date, s.stock, sp.price from generate_series('2016-01-01'::date, '2016-07-28', '1d') g(d) cross join ( select distinct stock from stock_prices ) s left join stock_prices sp on gd = sp.date and s.stock = sp.stock ) s order by 1, 2;
This is an alternative solution with a simple subquery:
select stock, date, price, ( select max(price) from stock_prices sp2 where sp2.stock = sp1.stock and sp2.date >= sp1.date- interval '90days' and sp2.date <= sp1.date ) highest_price from stock_prices sp1 order by 1, 2;
will be much more expensive. In this case, you should definitely use the index
create index on stock_prices (stock, date);
source share