I am looking for a 'vanilla' ANSI way of doing the following join. I would like to avoid any vendor specific syntax.
I have a table of securities descriptive data (STOCKS) and a table of daily stock prices (PRICES). I want to create a query (view) that will join the STOCKS table with the PRICES table such that the resulting view will contain a single row for each STOCKS row with columns for Current.Date, Current.Price, Prior.Date, Prior.Price where ‘Current’ is defined as the single row from PRICES with a date that is <= one defined date and ‘Prior’ is defined as the single row from PRICES with a date that is <= a second defined date.
STOCKS(Symbol, Name)
PRICES(Symbol, Date, Price)
STOCKS(‘MSFT’, ‘Microsoft Corporation’)
PRICES(‘MSFT’, ‘2009-03-31’, 18.375),
(‘MSFT’, ‘2009-02-27’, 16.150),
(‘MSFT’, ‘2008-12-31’, 19.44))
for example:
select …where CurrDate <= 2009-03-31 and PriorDate <= 2009-02-28… would return:
(‘MSFT’, 2009-03-31, 18.375, 2009-02-27, 16.150)
(note: 2009-02-28 was a Saturday. The <= allows the prior valid price to be returned)