I have schema similar to the following:
create table bar
(
instrument varchar(255) not null,
bar_dttm datetime not null,
bar_open int not null,
bar_close int not null
)
I would like to query the table, and return the most recent 5 rows per instrument.
I can do it instrument by instrument, with:
select top 5 instrument, bar_dttm, bar_open, bar_close
from bar
where instrument = 'XXX'
order by bar_dttm desc
I'd like to do this for all instruments at once in one query. Is this possible? I am running SQL Server 2008.