SQL Server 2005. Table schema is MarketdataID, Datatype, Date, Source, Coordinate, Value. PK is everything except Value. Data may be available from multiple sources and may not be available for the given date; I want to get the most recent date before the given date, and only one source per date.
SELECT top 1 [Source], [Date] FROM Market
WHERE MarketDataID = ?
AND DataType = ?
AND [Date] <= ?
order by [date] desc, [source]
then use the returned date and source in this query:
SELECT [Coordinate], [Value] FROM Market
WHERE MarketDataID = ?
AND DataType = ?
AND [Date] = ?
AND [Source] = ?
ORDER BY [coordinate]
1 Answer