天天看点

sqlserver 归组排序取值

表t_history中字段:dbid,filepath,baktype,createtime

要从这个表中取出dbid相同,以baktype作为查询条件,按createtime排序的第一条数据

表记录如图:

sqlserver 归组排序取值

使用了rownumber来排序的sql查询:

select * from
(select ROW_NUMBER() over(partition by dbid order by createtime desc) as rownum,* from T_History where baktype=1) t
where rownum=1