当前位置:网站首页>SQL gets the latest record of the data table

SQL gets the latest record of the data table

2022-04-23 20:21:00 First code

In the flow record table , A user usually generates many records if only the latest one , Can use max To get .

As shown in the file upload record table , Get the file upload record table , The latest record generated by each user :

select * from file_record as a inner join (
select file_author,max(file_ctime) as file_ctime  from file_record group by file_author) as b
on a.file_author=b.file_author 
and a.file_ctime=b.file_ctime

By using max、group by、inner join You can get the latest data record of each user ;

If you just need everyone's latest operation time , Just use b The content of :

select file_author,max(file_ctime) as file_ctime  from file_record group by file_author

Project instance :

select TableKey, full name , Department name , contact number , ID number  
from  essential information _ Summary  where  Effective or not =1 and ( Where  like @county+'%')) as a 
inner join (
select  ID number ,max( Meal time ) as  Meal time   
from [ Meal details summary ] where  Where  like @county+'%' and datediff(day, Meal time ,getdate())>=3 group by  ID number ) as b
on a. ID number =b. ID number 

If you want to say select Get the result set and insert it into other tables , It can be used :insert into #temp, Such as

insert into #tempOldman select TableKey, full name , Department name , contact number , Meal time  from (
select TableKey, full name , Department name , contact number , ID number  from  essential information _ Summary  
where  Effective or not =1 and ( Where  like @county+'%')) as a 
inner join (
select  ID number ,max( Meal time ) as  Meal time   from [ Meal details summary ] 
where  Where  like @county+'%' and datediff(day, Meal time ,getdate())>=3 group by  ID number ) as b
on a. ID number =b. ID number 

 

版权声明
本文为[First code]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204210551268217.html