I have a table with these values
ID, Name , LastModifedDate
1 , Tom , 01/01/2014
1 , Romain, 04/01/2014
2 , Paul , 01/01/2014
3 , John , 02/01/2014
and I need to get latest records like the following
1, Romain, 04/01/2014
2, Paul 01/01/2014
3, John, 02/01/2014
You can use row_number
to select the last modified row per ID
select * from (
select *,
row_number() over (partition by ID order by LastModifiedDate desc) rn
from mytable
) t1 where rn = 1
declare @t table (Id int,name varchar(10),lasmodified date)
insert into @t(Id,name,lasmodified) values (1,'tom','01/01/2014'),
(1,'Romain','04/01/2014'),(2,'paul','01/01/2014'),(3,'john','02/01/2014')
;with cte as
(select ID,name,lasmodified,row_number()OVER(PARTITION BY ID order by lasmodified desc) RN from @t)
select ID,name,MAX(CONVERT(VARCHAR(20),lasmodified,103)) from cte
WHERE RN = 1
group by ID,name
order by name desc
Do you mean all the records since Feb 01? Why should Paul be selected but not Tom?
SELECT ID, Name, LastModifiedDate
FROM YourTable
where LastModifiedDate > '01/01/2014'
Or do you want the last three??
SELECT TOP 3 ID, Name, LastModifiedDate
FROM YourTable
order by LastModifiedDate desc
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.