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

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM