我有类似于以下的模式:
create table bar ( instrument varchar(255) not null,bar_dttm datetime not null,bar_open int not null,bar_close int not null )
我想查询表,并返回最近的5行每个乐器.
我可以通过仪器做到这一点:
select top 5 instrument,bar_dttm,bar_open,bar_close from bar where instrument = 'XXX' order by bar_dttm desc
解决方法
交叉应用是你通常这样做 –
http://msdn.microsoft.com/en-us/library/ms175156.aspx
编辑 – 添加例子,像这样:
select bar1.instrument,bar2.* from ( select distinct instrument from bar) as bar1 cross apply ( select top 5 bar2.instrument,bar2.bar_dttm,bar2.bar_open,bar2.bar_close from bar as bar2 where bar2.instrument = bar1.instrument) as bar2
通常你想在那里添加一个订单.