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