日期:2014-05-17 浏览次数:20544 次
select * from (select *,ROW_NUMBER() over(partition by [集装箱号] order by [循环开始时间] desc) as rn from 表) t
where t.rn=1
create table test (id nvarchar(10),srq datetime,erq datetime)
insert into test
select 'SOC001','2012-01-01','2013-01-01' union all
select 'SOC001','2013-01-02','2014-01-01' union all
select 'SOC001','2014-01-02',null union all
select 'SOC002','2013-03-01','2014-02-01' union all
select 'SOC002','2014-02-02',null union all
select 'SOC003','2014-01-04',null
select *
from (
select *,
ROW_NUMBER() over(partition by id order by erq desc) as rn
from test
) t
where t.rn<=2 and t.erq is not null