SQLServer获取时间最新的记录

 

一、通过分组排序,加上一个序号列,获取最新记录(仅供参考)

select count(1) from 
    (
        select NCPYPCJSJ,dense_rank() over(order by NCPYPCJSJ desc) as OrderID 
        from NCPYPJCXXB 
        inner join TRJCDXXB  on NCPYPJCXXB.DWBH=TRJCDXXB.DWBH 
        --where QSDWDM=CODE and QSDWMC=NAME 
        and NCPYPJCXXB.NCPCBCD='1'
    )N where OrderID=1

 

二、摘自https://blog.csdn.net/stephenhendery/article/details/79236671

-- 方法1
select a.* 
 from table1 a
 where not exists(select 1 
                  from table1 b
                  where b.name=a.name and b.gdtime>a.gdtime)
 
-- 方法2
select a.*
 from table1 a
 inner join
 (select name,
         max(gdtime) 'maxgdtime'
  from table1 
  group by name) b on a.name=b.name and a.gdtime=b.maxgdtime

 

posted @ 2020-03-01 20:22  ParanoiaApe  阅读(2133)  评论(0)    收藏  举报